如何更新和删除gridview中的行 [英] how can i update and delete a row in gridview

查看:71
本文介绍了如何更新和删除gridview中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更新和删除gridview中的行。我有四个字段,分别是empid,empname,salary,city。

当我点击更新按钮时出现错误,表明没有指定更新命令。我可以在Gridview的gridview1_rowupdating事件中使用哪些代码。

解决方案

您好,



Plz通过链接:



http://www.aspdotnet-suresh.com /2011/02/how-to-inserteditupdate-and-delete-data.html [ ^ ]


这对我有用:





  protected   void  GridView1_RowUpdating( object  sender,GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows [e.RowInd EX];
string constr = ConfigurationManager.ConnectionStrings [ connstr]的ToString();
SqlConnection con = new SqlConnection(constr);

string id =(((Label)row.FindControl( < span class =code-string> lblid
))。Text).ToString();

string comments =(((TextBox)row.FindControl( < span class =code-string> txtcomments))。Text).ToString();
con.Open();
SqlCommand CmdSql = new SqlCommand( 更新请设置admin_comments =' + comments + ',grant =' + < span class =code-keyword> true + 'where id = + id + ,con);
CmdSql.ExecuteNonQuery();
GridView1.EditIndex = -1;
con.Close();
gridpop();

}


请遵循此代码..

  public   partial   class  DataTrialForm:Form 
{
private 字符串 connectionString = null ;
private SqlConnection sqlConnection = null ;
private SqlDataAdapter sqlDataAdapter = null ;
private SqlCommandBuilder sqlCommandBuilder = null ;
private DataTable dataTable = null ;
private BindingSource bindingSource = null ;
private 字符串 selectQueryString = null ;

public DataTrialForm()
{
InitializeComponent();
}

private void DataTraiForm_Load( object sender,EventArgs e)
{
sqlConnection = new SqlConnection( data source = SEZ-WS-137 \\SQLEXPRESS2008; initial catalog = AttendanceData; user id = sa; password = pass );

selectQueryString = 通过punchtime asc从出勤顺序中选择前20名 ;

sqlConnection.Open();

sqlDataAdapter = new SqlDataAdapter(selectQueryString,sqlConnection);
sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);

dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);

bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;

dataGridViewTrial.DataSource = bindingSource;

// 隐藏标识列
dataGridViewTrial.Columns [ 0 ]。可见= false ;
}

私有 void addUpadateButton_Click( object sender,EventArgs e)
{
try
{
sqlDataAdapter。更新(dataTable的);
}
catch (异常exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}

私有 void deleteButton_Click( object sender,EventArgs e)
{
try
{
dataGridViewTrial.Rows.RemoveAt(dataGridViewTrial.CurrentRow.Index);
sqlDataAdapter.Update(dataTable);
}
catch (异常exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}
}



另见..

http://www.mindstick.com/文章/ 9422cfc8-c2ed-4ec1-9fab-589eb850a863 /?插入%20Delete%20Update%20in%20DataGridView%20with%20DataTable%20in%20C [ ^ ]


how can i update and delete a row in gridview. i have four fields namely empid,empname,salary,city.
when i click on update button error shows that update command is not specified.what code can i use on gridview1_rowupdating event of gridview.

解决方案

Hi,

Plz go through link:

http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html[^]


This Worked for me:


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
            GridViewRow row = GridView1.Rows[e.RowIndex];
            string constr = ConfigurationManager.ConnectionStrings["connstr"].ToString();
            SqlConnection con = new SqlConnection(constr);

            string id = (((Label)row.FindControl("lblid")).Text).ToString();

            string comments = (((TextBox)row.FindControl("txtcomments")).Text).ToString();
            con.Open();
            SqlCommand CmdSql = new SqlCommand("Update leave set admin_comments='" + comments + "', granted='" + true + "' where id=" + id + " ", con);
                    CmdSql.ExecuteNonQuery();
                    GridView1.EditIndex = -1;
                    con.Close();
                    gridpop();

}


Follow this code..

public partial class DataTrialForm : Form
    {
        private String connectionString = null;
        private SqlConnection sqlConnection = null;
        private SqlDataAdapter sqlDataAdapter = null;
        private SqlCommandBuilder sqlCommandBuilder = null;
        private DataTable dataTable = null;
        private BindingSource bindingSource = null;
        private String selectQueryString = null;

        public DataTrialForm()
        {
            InitializeComponent();
        }

        private void DataTraiForm_Load(object sender, EventArgs e)
        {
          sqlConnection = new SqlConnection("data source=SEZ-WS-137\\SQLEXPRESS2008;initial catalog=AttendanceData;user id=sa;password=pass");

            selectQueryString = "select top 20 * from Attendance  order by punchtime asc";

            sqlConnection.Open();

            sqlDataAdapter = new SqlDataAdapter(selectQueryString, sqlConnection);
            sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);

            dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable);

            bindingSource = new BindingSource();
            bindingSource.DataSource = dataTable;

            dataGridViewTrial.DataSource = bindingSource;

           // to hide Identity column
            dataGridViewTrial.Columns[0].Visible = false;
        }

        private void addUpadateButton_Click(object sender, EventArgs e)
        {
            try
            {
                sqlDataAdapter.Update(dataTable);
            }
            catch (Exception exceptionObj)
            {
                MessageBox.Show(exceptionObj.Message.ToString());
            }
        }

       private void deleteButton_Click(object sender, EventArgs e)
        {
            try
            {
               dataGridViewTrial.Rows.RemoveAt(dataGridViewTrial.CurrentRow.Index);
               sqlDataAdapter.Update(dataTable);
            }
            catch (Exception exceptionObj)
            {
               MessageBox.Show(exceptionObj.Message.ToString());
            }
        }
    }


Also see it..
http://www.mindstick.com/Articles/9422cfc8-c2ed-4ec1-9fab-589eb850a863/?Insert%20Delete%20Update%20in%20DataGridView%20with%20DataTable%20in%20C[^]


这篇关于如何更新和删除gridview中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆