question

AndyNakamura-7969 avatar image
0 Votes"
AndyNakamura-7969 asked AndyNakamura-7969 commented

DataGridView. Handling selection changed event when column header is clicked

I have some code in the selection changed event for my datagridview control.
It just gets the value in a particular cell in the current row and puts the value in a textbox.



Try
TxtOpNotes.Text = DataGridView1.CurrentRow.Cells(10).Value
Catch ex As Exception

             MsgBox(ex.ToString)
         End Try

However, the app throws an exception if the user clicks on the column header:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

System.Windows.Forms.DataGridView.CurrentRow.get returned

I've tried

 If DataGridView1.CurrentRow.Index = -1 Then
             Exit Sub
         End If

But I get the same exception.

Anyone know how to get out of this. It doesn't seem possible to disable the column header mouse click


dotnet-csharpdotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered AndyNakamura-7969 commented

Try adding this line: If DataGridView1.CurrentRow Is Nothing Then Exit Sub.



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks Viorel!
I spent one and a half hours on that....
I can take the rope down now;-)

0 Votes 0 ·