在没有保存在DB中的RowDeleting的按钮单击中删除GridView Rows

时间:2014-01-28 05:37:34

标签: c# asp.net visual-studio-2010 gridview

我有一个gridview,

  1. 单击Button ADD时,文本框值将添加到gridview。
  2. 在gridview中添加值后,我想选择一行并单击“删除”按钮,但在删除时发生错误。
  3. 我的GridView代码

    <asp:GridView ClientIDMode="Static" ID="GridPesonal" runat="server" AutoGenerateColumns="False" Class="GrdSty" Width="100%" DataKeyNames="RowNumber">
        <Columns>
            <asp:BoundField DataField="RowNumber" HeaderText="ردیف">
            </asp:BoundField>
            <asp:BoundField DataField="StartDate" HeaderText="از تاریخ"> 
            </asp:BoundField>
            <asp:BoundField DataField="EndDate" HeaderText="تا تاریخ">
            </asp:BoundField>
            <asp:BoundField DataField="SherkatName" HeaderText="نام شرکت،سازمان یا موسسه" >
            </asp:BoundField>
            <asp:BoundField DataField="WorkUnitName" HeaderText="واحد محل کار"> 
            </asp:BoundField>
            <asp:BoundField DataField="Sharh" HeaderText="شرح دقیق شغل/سمت/مسئولیت" >
            </asp:BoundField>
            <asp:BoundField DataField="WorkTime" HeaderText="زمان کار" >
            </asp:BoundField>
            <asp:BoundField DataField="Description" HeaderText="توضیحات">
            </asp:BoundField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="SavabegSelect" runat="server" AutoPostBack="True" OnCheckedChanged="SavabegSelect_CheckedChanged" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EmptyDataTemplate>
            موردی ثبت نشده است.
        </EmptyDataTemplate>
    </asp:GridView>
    

    Btn删除代码

    <asp:Button ClientIDMode="Static" ID="BtnDelete" CssClass="btnregister"  runat="server" Text="حذف" OnClick="BtnDelete_Click"  />
    

    btn删除后面的代码

    protected void BtnDelete_Click(object sender, EventArgs e)
    {
         foreach (GridViewRow gvrow in GridPesonal.Rows)
         {
             CheckBox chkdelete = (CheckBox)gvrow.FindControl("SavabegSelect");
              if (chkdelete.Checked)
              {
                  GridPesonal.DeleteRow(gvrow.RowIndex);
              }
    
       } 
     }
    

    执行上述代码时,此错误显示为

    The GridView 'GridPesonal' fired event RowDeleting which wasn't handled.
    

2 个答案:

答案 0 :(得分:0)

您是否在Delete上使用了命令名BtnDeleteDelete Button删除它将起作用。

答案 1 :(得分:0)

如果你喜欢这个

<asp:GridView ID="gvRoute" DataKeyNames="Route" runat="server" AutoGenerateColumns="false"
                           AllowPaging="true" Width="30%" 
                          onrowdeleting="gvRoute_RowDeleting">

删除onrowdeleting="gvRoute_RowDeleting

的事件
protected void gvRoute_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
          \\
         }

或使用"CommandName"属性"Delete"

或使用OnClick

<asp:GridView ClientIDMode="Static" ID="GridPesonal" runat="server" AutoGenerateColumns="False" Class="GrdSty" Width="100%" DataKeyNames="RowNumber" OnClick="BtnDelete">