0

我正在尝试创建一个可编辑的 DataGridView,并且我已将“ AutoGenerateEditButton”启用为 true,但是当我单击页面上的编辑时,它会引发此异常

“System.Web.HttpException:GridView 'GridView1' 触发了未处理的事件 RowEditing。”

任何想法为什么?我正在使用的代码如下

我还想知道如何将编辑后的值更新到数据表中,以便更新。

html

<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True" Width="1060px">
</asp:GridView>

C#(代码隐藏)

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        add();
    }
}

private void add()
{
     DataTable dt = new DataTable();
     dt.Columns.Add("ab", typeof(string));
     dt.Columns.Add("ac", typeof(string));
     dt.Columns.Add("av", typeof(string));
     dt.Columns.Add("ax", typeof(string));
     DataRow row = dt.NewRow();
     row["ac"] = "sndasbfb";
     row["av"] = "sndasbfb";
     row["av"] = "sndasbfb";
     row["ax"] = "sndasbfb";
     dt.Rows.Add(row);
     GridView1.DataSource = dt;
     GridView1.DataBind();
}
4

3 回答 3

1

添加onrowediting事件..

<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True" 
                    Width="1060px" Onrowediting="Gridview_rowediting">

                </asp:GridView>


protected void Gridview_rowediting(object sender, GridViewEditEventArgs e)
{

}

要更新 gridview 添加 onrowupdating 事件..

protected void Gridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   update();

}
private void update()
{
 DataTable dt = new DataTable();
 dt.Columns.Add("ab", typeof(string));
 dt.Columns.Add("ac", typeof(string));
 dt.Columns.Add("av", typeof(string));
 dt.Columns.Add("ax", typeof(string));
 DataRow row = dt.NewRow();
 row["ac"] = "newvalue";
 row["av"] = "newvalue";
 row["av"] = "newvalue";
 row["ax"] = "newvalue";
 dt.Rows.Add(row);
 GridView1.DataSource = dt;
 GridView1.DataBind();
 }
于 2013-02-18T10:45:19.247 回答
0

您需要为 RowEditing 添加一个事件

在这里查看更多信息: http ://forums.asp.net/p/1144799/1850877.aspx#1850877


gvSalesEventSearch.RowEditing += new GridViewEditEventHandler(gvSalesEventSearch_RowEditing);

void gvSalesEventSearch_RowEditing(object sender, GridViewEditEventArgs e)
{

}
于 2013-02-18T10:44:59.307 回答
0

由于错误提示您尚未处理该OnRowEditing="GridViewEditEventHandler"事件。
如果默认edit link是点击gridview


您将需要OnRowEditing按以下方式处理

<asp:GridView ID="GridView1" runat="server" OnRowEditing="GridView1_RowEditing"
         AutoGenerateEditButton="True" Width="1060px">
</asp:GridView>

在后面的代码中,您必须按如下方式处理此事件

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
  //  GridView1.SelectedIndex = e.NewEditIndex;
      //do your stuff here
    add();
}
于 2013-02-18T11:00:56.387 回答