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

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
 
F

FUnky

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top