Hi,
To achieve the desired behavior, you can override the ProcessDataGridViewKey
and ProcessDialogKey
methods as you did, but with additional logic to handle the transition from column 5 to column 1 of the next row. You should also consider the CellValidating
event to ensure that the validation occurs before moving the focus.
Public Class mydgv
Inherits DataGridView
Dim ResetCol As Integer = 4 ' Index of the last editable column
Protected Overrides Function ProcessDialogKey(keyData As Keys) As Boolean
Dim key As Keys = keyData And Keys.KeyCode
If key = Keys.Enter Then
Return True
End If
Return MyBase.ProcessDialogKey(keyData)
End Function
Protected Overrides Function ProcessDataGridViewKey(e As KeyEventArgs) As Boolean
If e.KeyCode = Keys.Enter Then
If Me.CurrentCell IsNot Nothing AndAlso Me.IsCurrentCellInEditMode Then
If Not Me.EndEdit() Then
Return True ' Cancel if validation fails
End If
End If
Dim currPos As New Point(Me.CurrentCell.RowIndex, Me.CurrentCell.ColumnIndex)
If currPos.Y = Me.ResetCol Then
If currPos.X < Me.RowCount - 1 Then
Me.CurrentCell = Me.Rows(currPos.X + 1).Cells(0)
End If
Else
Me.CurrentCell = Me.Rows(currPos.X).Cells(currPos.Y + 1)
End If
Return True ' Suppress the default handling
End If
Return MyBase.ProcessDataGridViewKey(e)
End Function
End Class
Best Regards.
Jiachen Li
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.