网格值删除异常

时间:2013-07-17 10:59:50

标签: asp.net .net vb.net visual-studio-2008 gridview

我正在尝试使用以下代码从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

我不知道为什么会出现这种异常。

请帮帮我。

1 个答案:

答案 0 :(得分:1)

我认为你还没有在gridview中定义DataKeys属性,你应该在gridview中定义DataKeys="id" ..