You can do it in the event KeyDown
of DataGridView
: if the key pressed is the up or down arrow and the control key is pressed you cancel the event KeyPress
so that it does not execute the default behavior (go to the first or last row):
Private Sub DataGridView_KeyDown(sender As Object, e As KeyEventArgs)
If (e.Control AndAlso (e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.Down)) Then
e.SuppressKeyPress = True
End If
End Sub
As you point out in the comments this solution does not work if the EditMode
of DataGridView
is set to EditOnEnter
and you are editing a cell.
For that case the only solution I can think of is to create a custom control that inherits the DataGridView
and overwrite the function ProcessDataGridViewKey
replacing the functionality of Ctrl + Up
and Ctrl + Down
by Up
and% % co:
Public Class CustomDataGridView
Inherits DataGridView
<System.Security.Permissions.SecurityPermission( _
System.Security.Permissions.SecurityAction.LinkDemand, Flags:= _
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)> _
Protected Overrides Function ProcessDataGridViewKey( _
ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
If e.Control Then
If e.KeyCode = Keys.Up Then
Return ProcessUpKey(Keys.Up)
ElseIf e.KeyCode = Keys.Down Then
Return ProcessDownKey(Keys.Down)
End If
End If
Return MyBase.ProcessDataGridViewKey(e)
End Function
End Class
In your form you should include a Down
control instead of CustomDataGridView
. The new control will have the same functionality except the behavior of those two key combinations.