Question DataGridView cell navigation

AnthonyA

New member
Joined
Apr 7, 2010
Messages
2
Programming Experience
10+
I have a DataGridView with the StandardTab property set to False. I would like the cell pointer to "wrap around" when navigating beyond the last column. In other words, I would like to keep the cell pointer within the same row when using Tab, Shift+Tab, left and right arrow keys. Then move up and down each column using the up & down arrows.

The only method that has gotten me close is the PreviewKeyDown event. Here's my code but when the cell pointer is navigated beyond the last column, the pointer goes to the second column rather than the first.

Private Sub dgvMyGrid_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles dgvMyGrid.PreviewKeyDown

If dgvMyGrid.CurrentCell.ColumnIndex = dgvMyGrid.ColumnCount - 1 Then
dgvMyGrid.CurrentCell = dgvMyGrid.Item(0, dgvMyGrid.CurrentRow.Index)​
End If​

End Sub​


Thank you!
 
Quick and dirty solution

Although it's not "pretty", it works. :D

I simply added two "dummy buttons" to the form and placed them behind the DataGridView. I set the tab order of one dummy button immediately before the tab order of the grid and the other immediately after the grid. Then I used the GotFocus event for the dummy buttons to send the cell pointer back to the appropriate cell. It doesn't keep the pointer on the same row while tabbing through the grid (like I originally wanted) but it keeps the pointer within the grid when using Tab and Shift+Tab.

If someone has a better solution, I'm all for it! :D
Thanks!
 
Back
Top