GridViewUpdateEventArgs

GridViewUpdateEventArgs

我像这样绑定GridView-

namespace grid_edit
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data source=HP_OWNER\\SQLEXPRESS;Database=demo;Integrated security=true";
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from demo", con);
            cmd.ExecuteNonQuery();
            SqlDataAdapter mySqlAdapter = new SqlDataAdapter(cmd);
            DataSet myDataSet = new DataSet();
            mySqlAdapter.Fill(myDataSet);
            DataTable myDataTable = myDataSet.Tables[0];
            GridView1.DataSource = myDataTable;
            GridView1.DataBind();
            Session["mytable"] = myDataTable;


        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            GridView1.DataBind();

        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            String updatedItem = e.NewValues[0].ToString();
            String anotherUpdatedItem = e.NewValues[1].ToString();

        }

    }
}


并按照以下步骤进行标记-

<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True"
            BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px"
            CellPadding="4" CellSpacing="2" ForeColor="Black"
        style="top: 63px; left: 309px; position: absolute; height: 183px; width: 312px; margin-top: 25px;"
        OnRowEditing="GridView1_RowEditing"
        OnRowCancelingEdit="GridView1_RowCancelingEdit"
        OnRowUpdating="GridView1_RowUpdating" >
            <RowStyle BackColor="White" />
            <FooterStyle BackColor="#CCCCCC" />
            <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />

        </asp:GridView>


现在,我想从网格视图更新行,但是在更新事件GridViewUpdateEventArgs中,我得到了NewValues和OldValues为空。
如何获取更新的值以存储在数据库中?我在这里做错什么了吗?

最佳答案

尝试如下更改代码:

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data source=(local);Database=northwind;Integrated security=true";
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from Categories", con);
        cmd.ExecuteNonQuery();
        SqlDataAdapter mySqlAdapter = new SqlDataAdapter(cmd);
        DataSet myDataSet = new DataSet();
        mySqlAdapter.Fill(myDataSet);
        DataTable myDataTable = myDataSet.Tables[0];
        GridView1.DataSource = myDataTable;
    if(!IsPostBack)  /// <<<<<<<<<<<<<<<<<<<<<<
        GridView1.DataBind();
        Session["mytable"] = myDataTable;

    }


对于.NET Framework 4,它在这里工作正常。

由于您使用的是.NET Framework 3.0,因此此代码不起作用。我查看了GridView的HandleUpdate方法,发现仅当Gridview通过DataSourceID属性绑定到DataSourceControl时才填充这些集合(e.NewValues和e.OldValues)。一种可行的解决方案是使用ExtractValuesFromCell方法:

    cell = GridView1.Rows[e.RowIndex].Cells[1] as DataControlFieldCell;
    GridView1.Columns[1].ExtractValuesFromCell(
        e.NewValues,
        cell,
        DataControlRowState.Edit,
        true);


注意:Page_Load方法应具有我建议的代码,即DataBind仅应调用一次。

关于c# - 为什么我的GridViewUpdateEventArgs#NewValues和GridViewUpdateEventArgs#OldValues为空?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6641474/

10-12 04:04