简体   繁体   中英

How can I delete entire row in my SQL datagridview?

How can I delete an entire row in my datagridview.. I already have a delete link in my datagrid.. Here is my markup code in 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>

And when I click the delete link this error shows

"The GridView 'EmployeeHallway' fired event RowDeleting which wasn't handled. "

Can anyone help me what to do next

You are using Delete as the CommandName for the delete link so it will automatically creates a RowDeleting event. So you have to implement it like this:

You have to add OnRowDeleting event as below:

<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"/>

And at the code-behind:

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

End Sub

Add OnRowDeleting="OnRowDeleting" in aspx page

Saving Your DataTable in ViewState("dt") and then Do Like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM