0

I have the following code...

 Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.RowUpdating
     Dim SocioNumInfo As Integer = CInt(GridView1.Rows(GridView1.SelectedIndex).Cells(4).Text)
     MsgBox(SocioNumInfo.ToString)
     ...
 End Sub

Now, this code should read the cell, but it gives me the following error:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

The MsgBox is just there for me to check if the data is being read, at the end of the day it should turn that into a parameter so I can add to the DB... but still.. nope nothing. Is that the correct code to read directly from a cell in a choosen row? In the "Protected Sub" area I already tried SelectedIndexChanging and RowEditing, etc... and still nothing. Still throws the error at me.

If I try Dim SocioNumInfo As String = CStr(GridView1.Rows(GridView1.SelectedRow).Cells(4).Text) it gives me a "cannot be converted to Integer" error.

4

1 回答 1

0

如果没有选择行,GridView1.SelectedIndex则为-1。您可能需要添加支票

If GridView1.SelectedIndex <> -1 Then
    ' Use GridView1.SelectedIndex here
End If

但是,像这样访问选定的行会更容易:

Dim row = GridView1.SelectedRow
If Not row Is Nothing AndAlso IsNumeric(row.Cells(2).Text) Then
    Dim SocioNumInfo As Integer = CInt(row.Cells(2).Text)
    ....        
End If

请注意,单元格索引是从零开始的。row.Cells(4)位于第 5 列。

于 2013-03-26T19:31:50.033 回答