vb.net 如何访问在 RowDeleting 事件中被删除的行的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15771282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:05:18  来源:igfitidea点击:

How to access values of row being deleted in RowDeleting event

asp.netvb.netgridviewrowdeleting

提问by BearSkyview

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.

在从数据库中删除之前,我试图保存一些信息。我正在使用asp gridview。我正在尝试从使用 RowDeleting 事件删除的行中检索值。然而,我一直非常不成功。

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:

a 最终成为标题行第一列。b 错误输出。我尝试了很多不同的东西,但无法提出行索引。我会认为这个事件只会访问被删除的行。这是我的 gridview 声明:

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

回答by Anna Forrest

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

声明eSystem.Web.UI.WebControls.GridViewDeleteEventArgs. 然后,您可以使用e.RowIndex.

回答by Ramesh Rajendran

Try this sample way like use RowCommand

尝试使用这种示例方式 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

并在 vb 文件中更改

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.

注意:确保您仍然有 Row_deleting 事件处理程序方法,但 Empty 没有任何代码。否则它会给你错误。