i have a data grid,, connected in ms access via code, on its celldoubleclick event i have the code

Public Sub dgv1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv1.CellDoubleClick
   Dim empid As Object = dgv1.Rows(e.RowIndex).Cells(0).Value
   display.Text = Convert.ToString(empid)
   editempfrm.ShowDialog()
 End Sub

it suppose to display its content to the next form for editing.
the problem is, when the header,the row containing the title, were double clicked i got this error

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

is there any way to make this work properly?

You can't display the header using RowIndex.
I think RowIndex is negative when you double click the header. Test it and if it is use this as a check to determine that the event comes from the header.
If it does exit sub without showing the next form as you can't edit anything.

You can't display the header using RowIndex.
I think RowIndex is negative when you double click the header. Test it and if it is use this as a check to determine that the event comes from the header.
If it does exit sub without showing the next form as you can't edit anything.

ahm sir is there a way that i can disable that header,,

you don't need to disable the header. In your code before line two put some code like

if e.RowIndex < 0 then
  msgbox "Please double click on the cell you are interested in"
  exit sub
end if

it is better to provide instructive feedback to your user than simply disable things.

it is better to provide instructive feedback to your user than simply disable things.

Yes, but then they wouldn't pay us to "fix" the not-working-thing, only to end up with a silly message box. :D Just kidding.
Generally I agree, but then again I've answered hundrends of calls with a user saying that they got an error, that they can't read to me or tell me what it was about because they clicked OK (or Cancel or "a button and now it's gone").

I can't remember where I've read it, but I always remember: "Make it idiot-proof and somebody will make a better idiot".

Well if we are going to be that thorough, I generally have a subroutine that both displays the msgbox and writes the error to a log file. I then have a list box 1 line deep that displays a chronological descending list of errors displayed to the user.

below is MSAccess code but you get the idea

Public Sub SetMessage(MessageText As String, MessageType As String, Optional SourceTable As String, Optional SourceRecId As Long)
Dim Work As String
Dim RecordId As Long

Set thisdb = CurrentDb()
If IsMissing(SourceRecId) Then RecordId = 0 Else RecordId = SourceRecId
MsgBox MessageText
Work = Convert(MessageText, "'", Chr$(34))
If IsMissing(SourceTable) Then
    CurrentDb().Execute "INSERT INTO MessageHistory (MessageType, Message, MessageDateTime) SELECT '" & MessageType & "' AS MessageType, '" & Work & "' As Message, Now() AS MessageDateTime;"
Else
    CurrentDb().Execute "INSERT INTO MessageHistory (MessageType, Message, MessageDateTime, SourceTableName, SourceRecordId) SELECT '" & MessageType & "' AS MessageType, '" & Work & "' As Message, Now() AS MessageDateTime, '" & SourceTable & "' AS SourceTableName, " & RecordId & " AS SourceRecordId;"
End If
Forms![MainScreen]![lstMessage].Requery
End Sub

you don't need to disable the header. In your code before line two put some code like

if e.RowIndex < 0 then
  msgbox "Please double click on the cell you are interested in"
  exit sub
end if

it is better to provide instructive feedback to your user than simply disable things.

sir i put the code yet get the same error.. i just wanted a simple code at first sir however ill try to study the one you have given above but as of now sir how can execute the program with only using a message box

its working now sir thank you... :)

The code in VBA you will need to adjust it for .Net

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.