Question Argument out of range not handled

JamesBowtell

Member
Joined
Feb 26, 2009
Messages
16
Programming Experience
3-5
Hey

i have a form that contains a DataGrid that displays information about a user.

I'm getting an error every time i click a header on this data grid but i dont know why.

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

VB.NET:
    Public Sub Tbl_userDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Tbl_userDataGridView.CellClick

        NuNameTB = Me.Tbl_userDataGridView.Rows(e.RowIndex).Cells("DataGridViewTextBoxColumn1").Value
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()

    End Sub

what the above code does is select the users id from the users column and sets a variable with that value.

i'm using Visual Basic 2008 express, is there anyway of stopping users being able to click the headers in a datagrid. I have set AllowUsersToResizeColumns to False

any ideas ?

thanks
 
short of setting ColumnHeadersVisible = False, you could in your code for the CellClick event check the e.RowIndex variable for -1, this will be a column header and you could skip the code for a normal cell.
 
VB.NET:
Public Sub Tbl_userDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Tbl_userDataGridView.CellClick

     'So if the cell rowindex is greater than or equal to zero, then perform the code
     If e.RowIndex >= 0  Then
        NuNameTB = Me.Tbl_userDataGridView.Rows(e.RowIndex).Cells("DataGridViewTextBoxColumn1").Value
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
     End If

End Sub
 

Latest posts

Back
Top