Asp.net使用AutoGenerateEditButton更新GridView

时间:2013-04-12 15:16:56

标签: c# asp.net datagrid insert

如何使用AutoGenerateEditButton更新我的gridview表(绑定了一个数据集-Dataset从sql数据库中检索)

enter image description here

- 删除问题中的破碎代码,将固定代码放入答案 -

1 个答案:

答案 0 :(得分:1)

要获取更新行的值,请将其添加到“RowUpdating”事件处理程序

protected void grdViewDetails_RowUpdating(object sender,GridViewUpdateEventArgs e)         {

        GridViewRow row = (GridViewRow)grdViewDetails.Rows[e.RowIndex];

        foreach (Control item in row.Controls)
        {
            if (item.Controls[0] is TextBox)
            {
                TextBox textbox = (TextBox)item.Controls[0];
                string x = textbox.Text; //theres your value you can do stuff with
            }
            if (item.Controls[0] is Label)
            {
                Label mylabel = (Label)item;
                //do stuff - just do the same as the textbox
            }
        }

}

和“RowEditing”事件处理程序

 protected void grdViewDetails_RowEditing1(object sender, GridViewEditEventArgs e)
        {
            grdViewDetails.EditIndex = e.NewEditIndex;
            //e.newedit index:- will be provide index of row for which edit button is selected
            grdViewDetails.DataSource = yourdatasource //mine was a datset
            grdViewDetails.DataBind();
        }