Hi Tigerwood2006,
Usually, we create DataGridView by dragging a DataGridView control from ToolView into our Form.
But in your case, we need to manually create a inheritated custom DataGridView, let’s call it MyDataGridView. It means you have to define a new class called MyDataGridView which inheretated from DataGridView.
There are two main steps to achieve this goal:
1. Define a custom class MyDataGridView, inheretated from DataGridView. In MyDataDridView, you need not change any other code except method ProcessDialogKey and method OnKeyDown. Here is the code:
Code Snippet
Class myDataGridView
Inherits DataGridView
'
' Other code in your datagridview class.
'
Protected Overloads Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
If keyData = Keys.Enter Then
Dim col As Integer = Me.CurrentCell.ColumnIndex
Dim row As Integer = Me.CurrentCell.RowIndex
If row <> Me.NewRowIndex Then
If col = (Me.Columns.Count - 1) Then
col = -1
row += 1
End If
Me.CurrentCell = Me(col + 1, row)
End If
Return True
End If
Return MyBase.ProcessDialogKey(keyData)
End Function
Protected Overloads Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
If e.KeyData = Keys.Enter Then
Dim col As Integer = Me.CurrentCell.ColumnIndex
Dim row As Integer = Me.CurrentCell.RowIndex
If row <> Me.NewRowIndex Then
If col = (Me.Columns.Count - 1) Then
col = -1
row += 1
End If
Me.CurrentCell = Me(col + 1, row)
End If
e.Handled = True
End If
MyBase.OnKeyDown(e)
End Sub
End Class