如何在SQL数据网格视图中删除整行?

时间:2014-11-03 05:02:22

标签: asp.net sql-server database vb.net vba

如何在datagridview中删除整行..我的数据网格中已经有删除链接.. 这是我在vb中的标记代码

 <asp:GridView ID="EmployeeHallway" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="False" AutoGenerateSelectButton="True" Height="93px" HorizontalAlign="Center" PageSize="6" style="margin-bottom: 0px; text-align: center;" Width="768px">
              <AlternatingRowStyle BackColor="White" />
              <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>

                               <%--ADD THE DELETE LINK BUTTON--%>
                               <asp:LinkButton ID="LinkButton2" Runat="server" OnClientClick ="return confirm('Are you sure you?');"
                                   CommandName="Delete">Delete</asp:LinkButton>

                        </ItemTemplate>
                     </asp:TemplateField>
                  <asp:BoundField DataField="EmployeeID" HeaderText="Locker ID" />
                  <asp:BoundField DataField="Name" HeaderText="Name" />
                  <asp:BoundField DataField="EmployeeNo" HeaderText="EmployeeNo" />
                  <asp:BoundField DataField="Email" HeaderText="Email" />
                  <asp:BoundField DataField="Location" HeaderText="Location" />
              </Columns>
              <HeaderStyle BackColor="#003366" BorderColor="#336699" BorderStyle="Solid" ForeColor="White" />
              <PagerStyle BackColor="#003366" BorderColor="#336699" ForeColor="White" />
              <RowStyle BackColor="White" ForeColor="#003366" />
              <SelectedRowStyle BackColor="White" ForeColor="#6600FF" />
              <SortedAscendingCellStyle BackColor="#CCCCCC" />
 </asp:GridView>

当我点击删除链接时,此错误显示

  

&#34; GridView&#39; EmployeeHallway&#39;没有处理过的事件RowDeleting。 &#34;

任何人都可以帮我下一步做什么

2 个答案:

答案 0 :(得分:1)

您使用Delete作为删除链接的CommandName,因此它会自动创建RowDeleting个事件。所以你必须像这样实现它:

您必须添加OnRowDeleting事件,如下所示:

<asp:GridView ID="EmployeeHallway" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="False" AutoGenerateSelectButton="True" Height="93px" HorizontalAlign="Center" PageSize="6" style="margin-bottom: 0px; text-align: center;" Width="768px">
    <RowStyle ForeColor="#003399" HorizontalAlign="Center" OnRowDeleting="EmployeeHallway_RowDeleting"/>

在代码隐藏处:

Public Sub EmployeeHallway_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)

End Sub

答案 1 :(得分:0)

在aspx页面中添加OnRowDeleting="OnRowDeleting"

将您的DataTable保存在ViewState("dt")中,然后执行此操作:

Protected Sub OnRowDeleting(sender As Object, e As GridViewDeleteEventArgs)
    Dim index As Integer = Convert.ToInt32(e.RowIndex)
    Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
    dt.Rows(index).Delete()
    ViewState("dt") = dt
    BindGrid()
End Sub

 Protected Sub BindGrid()
    EmployeeHallway.DataSource = TryCast(ViewState("dt"), DataTable)
    EmployeeHallway.DataBind()
End Sub