Link to home
Start Free TrialLog in
Avatar of holemania
holemania

asked on

VB.NET Datagridview - Index out of range error

I have a datagridview that is working fine.  However, I set a CellDoubleClick to do an event and everytime I accidently click on the header of a column, I get "Index was out of range.  Must be non-negative and less than the size of the collection.  Parameter name:  index".

I would I set it so that it would either exit the event or not give me an unhandled error?  Below is my attempt to exit sub if they click on header, but that's where it error out with the index out of range.
If dgViewDetail.Rows(e.RowIndex).Cells("ID").Value = 0 Then
  Exit Sub
End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of holemania
holemania

ASKER

Awesome.  Thank you!!
Try it like this.
    Private Sub dgViewDetail_CellDoubleClick(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgViewDetail.CellDoubleClick
 
        If e.RowIndex < 0 Then Return
 
        If dgViewDetail.Rows(e.RowIndex).Cells("ID").Value = 0 Then
            Exit Sub
        End If
 
    End Sub

Open in new window