可编辑数据网格视图抛出异常

时间:2013-02-18 10:41:06

标签: c# asp.net datagridview

我正在尝试创建一个可编辑的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();
}

3 个答案:

答案 0 :(得分: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();
 }

答案 1 :(得分:0)

您需要为RowEditing添加一个事件

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


gvSalesEventSearch.RowEditing += new GridViewEditEventHandler(gvSalesEventSearch_RowEditing);

void gvSalesEventSearch_RowEditing(object sender, GridViewEditEventArgs e)
{

}

答案 2 :(得分: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();
}