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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Place this check
If e.RowIndex >= 0 then
   'remain code here
End if

Headers are at index -1.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
holemaniaAuthor Commented:
Awesome.  Thank you!!
Fernando SotoRetiredCommented:
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.