1

I am trying to save a little bit of information before I delete it from the database. I'm using an asp gridview. I'm trying to retrieve the values from the row being deleted with the RowDeleting event. However, I've been extremely unsuccessful.

Protected Sub gv1_RowDeleting(sender As Object, e As System.EventArgs) Handles   gv1.RowDeleting
   Dim a As String = gv1.Columns(0).ToString
   Dim b As String = gv1.SelectedRow.RowIndex.ToString
   Dim c As String = gv1.Rows(a).Cells(0).Text
End Sub

a ends up being the header row first column. b errors out. I've tried a bunch of different things and can't come up with a row index. I would have figured this event would access only the row being deleted. Here is my gridview declaration:

<asp:GridView ID="gv1" runat="server" AllowSorting="True" AutoGenerateColumns="False" EnableViewState="false"
            DataSourceID="ds1" EmptyDataText="NO ROWS FOUND" CssClass="gridView" DataKeyNames="id" GridLines="None">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="ID" SortExpression="id" />
                <asp:BoundField DataField="totalsd" HeaderText="SD" SortExpression="totalsd" />
                <asp:BoundField DataField="freesd" HeaderText="FREE" SortExpression="freesd" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="deleteButton" runat="server" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
BearSkyview
  • 385
  • 1
  • 8
  • 24

2 Answers2

2

Declare e as System.Web.UI.WebControls.GridViewDeleteEventArgs. You can then get the row index with e.RowIndex.

Druid
  • 6,423
  • 4
  • 41
  • 56
Anna Forrest
  • 1,711
  • 14
  • 21
1

Try this sample way like use RowCommand

<asp:LinkButton ID="DelButton" runat=server Text="Delete" CommandName="Delete" CausesValidation=False CommandArgument='<%# Databinder.Eval(Container,"DataItem.id") %>'></asp:LinkButton>

and in vb file Change

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

        Dim MyConn1 As New OleDbConnection(Your Connection string)
        Dim cmdDelete As OleDbCommand

        If e.CommandName = "Delete" Then
             cmdDelete = New OleDbCommand("Delete from table" & _
                                               "Where id= " & e.CommandArgument.ToString() & ";", MyConn1)
            MyConn1.Open()
            cmdDelete.ExecuteNonQuery()
            MyConn1.Close()
            BindAutoMake()

        End If
    End Sub 

N.B : Make sure you still have Row_deleting event handler method but Empty without any code. Otherwise it will gives you error.

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234