How do I control navigation in datagridview. ie to go sideways on <enter>
When the user presses enter in a cell for a certain column I want to sent the next cell to the next column, like doing a <tab> instead of going to the next row.
I have used the keypress event, but this is not fired if the user is in edit mode and presses enter.
Anyother action to set currentcell changes the column, but the process still moves to the next row.
Does anyone have any ideas.
Hi David,
You can control the navigation by handling the KeyDown event on your DataGridView. It's important to set the KeyEventArgs.Handled property to True so that the event is not passed down to the default handler for the DataGridView. Here is an example using a DataGridView named CustomersDataGridView:
| | Private Sub CustomersDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CustomersDataGridView.KeyDown If e.KeyCode = Keys.Enter Then If CustomersDataGridView.CurrentCell.ColumnIndex = CustomersDataGridView.ColumnCount - 1 Then If CustomersDataGridView.CurrentCell.RowIndex < CustomersDataGridView.RowCount - 1 Then CustomersDataGridView.CurrentCell = CustomersDataGridView.Item(0, CustomersDataGridView.CurrentCell.RowIndex + 1) End If Else CustomersDataGridView.CurrentCell = CustomersDataGridView.Item(CustomersDataGridView.CurrentCell.ColumnIndex + 1, CustomersDataGridView.CurrentCell.RowIndex) End If End If e.Handled = True End Sub
|
There are a also couple of checks in the code to prevent you from setting the cell index outside the bounds of the grid.
I hope this helps.
Jay Hickerson
Visual Basic Team
Hi Jay
I tried this out, but the keypress and keydown events do not fire when the user presses enter in edit mode which is when I want to move to the next cell..
The only way I can seem to achieve this is to set a flag in the cellvaluechanged event and then when I hit enter the rowvalidating event fires and I set the e.cancel to abort the row change.
Not elegant but appears to work. I don't know if there is a better way
Regards
David Jordan
(VS2005 - beta 2)
David, I realize this is an old post but perhaps you can help me. I'm trying to accomplish the same thing (only in C#) and I have implemented your idea of using the CellValidating event, but I'm having nothing but trouble. The enter key works how it should, but not the other keys. How are you determining which key is being pressed? From what I can tell, your solution will still have problems if an arrow key is pressed. In fact, I can't see anyway to capture a key stroke in an editable datagrid - the KeyDown/KeyUp event doesn't ever get executed.
Thanks
-Adam
Ok, here goes nothing...
Dim LastEditedCell as DataGridViewCell
Private Sub DGVCenter_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVCenter.CellEndEdit
LastEditedCell = Me.DGVCenter.CurrentCell
End Sub
Private Sub DGVCenter_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVCenter.CellEnter
If Not LastEditedCell Is Nothing Then
If (Me.DGVCenter.CurrentCell.RowIndex = LastEditedCell.RowIndex + 1) And (Me.DGVCenter.CurrentCell.ColumnIndex = LastEditedCell.ColumnIndex) Then
Dim Asunto As New Threading.Thread(AddressOf MueveACelda)
Asunto.Start()
End If
End If
End Sub
Sub MueveACelda()
Try
Dim r, c As Short
r = LastEditedCell.RowIndex
c = LastEditedCell.ColumnIndex
LastEditedCell = Nothing
Me.DGVCenter.CurrentCell = Me.DGVCenter.Rows(r).Cells(c + 1)
Catch Ex As Exception
' MsgBox(Ex.Message)
End Try
End Sub