Asp.Net 在带有自动生成列的 GridView 中的代码隐藏编辑值中查找

Asp.Net finding in Code-behind edited values in GridView with autogenerated columns

我正在尝试创建一个 gridview 的用户控件,它将能够显示、编辑和删除记录,绑定到任何数据表。 在我的用户控件中,我有:

<asp:GridView ID="EditableGrid" runat="server" Width="500px" Height="500px" AllowSorting="True"
                AutoGenerateColumns = "true"
       AutoGenerateDeleteButton   ="true" AutoGenerateEditButton="true"
                OnRowEditing="EditableGrid_RowEditing" 
                OnRowCancelingEdit="EditableGrid_RowCancelingEdit"
                OnRowUpdating="EditableGrid_RowUpdating"
                OnRowDeleting="EditableGrid_RowDeleting"

                 ></asp:GridView> 

在我后面的代码中有:

public void InitGrid(string theconnstr, string thetablename)
        {
            connstr = theconnstr;
            tablename = thetablename;
          //  Session["edgconnstr"] = connstr;
          //  Session["edgtablename"] = tablename;
            con = new SqlConnection(connstr);
            con.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandText = "SELECT * FROM " + tablename;
                using (SqlDataReader rd = cmd.ExecuteReader())
                {
                    if (!rd.HasRows) return;
                    fields = new List<EdField>();
                    for (int i =0; i < rd.FieldCount; i++)
                    {
                        fields.Add(new EdField(rd.GetName(i), rd.GetDataTypeName(i)));
                    }
                }
            }
            con.Close();
        }

        public void Bind()
        {
         //   connstr = (String)Session["edgconnstr"];
          //  tablename = (String)Session["edgtablename"];
            con = new SqlConnection(connstr);
            con.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandText = "SELECT * FROM " + tablename;
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        da.Fill(dt);
                        EditableGrid.DataSource = dt;
                        EditableGrid.DataBind();
                        EditableGrid.Visible = true;
                    }
                }
            }
            con.Close();
        }


        protected void EditableGrid_RowEditing(object sender, GridViewEditEventArgs e)
        {
            EditableGrid.EditIndex = e.NewEditIndex;
            Bind();
        }

        protected void EditableGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            EditableGrid.EditIndex = -1;
            Bind();
        }


        protected void EditableGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
        }


        protected void EditableGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {


        }

现在我尝试在 EditableGrid_RowUpdating 事件处理程序中找到已编辑行的新值,但没有成功。我只是无权访问文本框,因此我无法 运行 将旧值更新为新值的查询。我想达到的目标是否可行?

使用 e.NewValues Collection.

protected void EditableGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int tx = Convert.ToInt32(e.NewValues[0]);
}