Unslect a row in DataGridView

S

Stephen Hatfield

I have a Windows Forms application consisting of several main level
TabControls and each of these controls has two or more TabControls within
them.

I have a method which calls all the LoadDataGridViews methods which I call
in the form's constructor. The last line in each method is
datagridview.ClearSelection().

These methods also get called in a TabControl_Selected event handler.

The first time I go into a given tab, all DataGridViews have the first row
selected. If I click out and click back into a given tab, the first rows are
now unselected.

I have also tried ClearSelection for each grid individually within a
Form_Load event with the method for loading all the grids within it as well.

Debugging shows the method is called either way yet the first row is always
selected the first time the grid is displayed.

Any suggestions would be most appreciated!

Thanks!
Stephen
 
J

Jack Jackson

I have a Windows Forms application consisting of several main level
TabControls and each of these controls has two or more TabControls within
them.

I have a method which calls all the LoadDataGridViews methods which I call
in the form's constructor. The last line in each method is
datagridview.ClearSelection().

These methods also get called in a TabControl_Selected event handler.

The first time I go into a given tab, all DataGridViews have the first row
selected. If I click out and click back into a given tab, the first rows are
now unselected.

I have also tried ClearSelection for each grid individually within a
Form_Load event with the method for loading all the grids within it as well.

Debugging shows the method is called either way yet the first row is always
selected the first time the grid is displayed.

Any suggestions would be most appreciated!

My experience suggests that the first time a DataGridView is
displayed, either for the first time since it was created or when its
Visible property is changed from False to True, it will show the
current record as selected.

You need to arrange to call ClearSelection() after the grid has been
displayed.

I have had success with this code, which tracks the CurrentCellChanged
and VisibleChanged events and tries to determine when the selection
has been set due to the visibility changing:

Private m_clearCurrentCell as Boolean = False
Private m_lastVisible as Boolean = False

Protected Overrides Sub OnCurrentCellChanged(ByVal e As
System.EventArgs)
MyBase.OnCurrentCellChanged(e)

' If we have a CurrentCell, control is now visible, and
control was not
' visible the last time we got a VisibleChanged event, that
means that
' CurrentCell changed due to Visible changing from False to
True, so we
' set CurrentCell to Nothing.
If CurrentCell IsNot Nothing AndAlso Visible AndAlso Not
m_lastVisible Then
m_clearCurrentCell = True
End If
End Sub

Protected Overrides Sub OnVisibleChanged(ByVal e As
System.EventArgs)
MyBase.OnVisibleChanged(e)

If Visible And m_clearCurrentCell Then
ClearSelection()
End If
m_clearCurrentCell = False
m_lastVisible = Visible
End Sub
 

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