我正在尝试使用以下代码从 VB.NET 中的网格中删除值:
Protected Sub gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gv.RowDeleting
Dim index As Integer = gv.EditIndex
Dim row As GridViewRow = DirectCast(gv.Rows(e.RowIndex), GridViewRow)
Dim id As Integer = Convert.ToInt32(gv.DataKeys(e.RowIndex).Value.ToString())
'Dim Id As Integer = Integer.Parse(DirectCast(gv.Rows(e.RowIndex).FindControl("txtId"), TextBox).Text)
gc.ExecuteCommand("delete from expence where id= '" & Id & "' ")
Response.Write("<script type='text/javascript' language='javascript'>alert('Data Updated')</script>")
gv.EditIndex = -1
bindGrid()
End Sub
它在网上给了我一个例外,Dim id As Integer = Convert.ToInt32(gv.DataKeys(e.RowIndex).Value.ToString())
如下所示:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
我不知道为什么会出现这个异常。
请帮我。