How to avoid selecting column header row in DataGridView????

  • Thread starter Thread starter Hexman
  • Start date Start date
H

Hexman

Hello All,

I have a DataGridView that I want to use only for item selection. I
have set the following properties on the dgv.

MultiSelect = False
ReadOnly = True
RowHeadersVisible = False
SelectionMode = FullRowSelect

I'm using the DataGridView SelectionChanged Event to perform some
calcs, fill textboxes, etc. Everything is working fine until I click
on a column heading (to sort by that column). It fails on the "If
cXROW...." line in the code below with the message "Object reference
not set to an instance of an object.". cXROW is equal to 'Nothing'.

I've checked the events and the SelectionChanged seemed the best for
this.
How can I avoid this error? Allow column sorting with FullRowSelect?

Thanks,

Hexman


Private Sub dgvPP_SelectionChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles dgvPP.SelectionChanged

Dim cXRow As DataGridViewRow
cXRow = dgvPP.CurrentRow
If cXRow.Cells("RAFlag").Value = "1" Then
....Do Something....
End If

End Sub
 
You could do something like this ...
Dim cXRow As DataGridViewRow
cXRow = dgvPP.CurrentRow
If Not (cXRow Is Nothing)Then
If cXRow.Cells("RAFlag").Value = "1" Then
....Do Something....
End If
End if
 
Back
Top