Try adding this line: If DataGridView1.CurrentRow Is Nothing Then Exit Sub.
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