0

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

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
Patrick Lopez
  • 51
  • 1
  • 1
  • 3
  • possible duplicate of [The GridView 'PendingRecordsGridview' fired event RowDeleting which wasn't handled](http://stackoverflow.com/questions/14301815/the-gridview-pendingrecordsgridview-fired-event-rowdeleting-which-wasnt-handl) – Vishal Suthar Nov 03 '14 at 05:07

2 Answers2

1

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
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
0

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
Dgan
  • 10,077
  • 1
  • 29
  • 51