|
1 | | -# how-to-change-the-Enter-key-behavior-in-winforms-datagrid |
2 | | -This example illustrates how-to-change-the-Enter-key-behavior-in-winforms-datagrid |
| 1 | +# How to Change the Enter Key Behavior in WinForms DataGrid? |
| 2 | + |
| 3 | +This example illustrates how to change the Enter key behavior in [WinForms DataGrid](https://www.syncfusion.com/winforms-ui-controls/datagrid) (SfDataGrid). |
| 4 | + |
| 5 | +When pressing the Enter key, the current cell will be moved to the next row in DataGrid, by default. You can change this behavior like, Tab key that moves the current cell to the next cell on the same row by writing a custom [RowSelectionController](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGrid.Interactivity.RowSelectionController.html) overriding method [HandleKeyOperations](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGrid.Interactivity.SelectionControllerBase.html#Syncfusion_WinForms_DataGrid_Interactivity_SelectionControllerBase_HandleKeyOperations_System_Windows_Forms_KeyEventArgs_). |
| 6 | + |
| 7 | +### C# |
| 8 | + |
| 9 | +``` csharp |
| 10 | +public Form1() |
| 11 | +{ |
| 12 | + InitializeComponent(); |
| 13 | + this.sfDataGrid.SelectionController = new CustomSelectionController(sfDataGrid); |
| 14 | +} |
| 15 | + |
| 16 | +public class CustomSelectionController : Syncfusion.WinForms.DataGrid.Interactivity.RowSelectionController |
| 17 | +{ |
| 18 | + SfDataGrid DataGrid; |
| 19 | + |
| 20 | + public CustomSelectionController(SfDataGrid sfDataGrid) : base(sfDataGrid) |
| 21 | + { |
| 22 | + this.DataGrid = sfDataGrid; |
| 23 | + } |
| 24 | + |
| 25 | + protected override void HandleKeyOperations(KeyEventArgs args) |
| 26 | + { |
| 27 | + if (args.KeyCode == Keys.Enter) |
| 28 | + { |
| 29 | + KeyEventArgs arguments = new KeyEventArgs(Keys.Tab); |
| 30 | + base.HandleKeyOperations(arguments); |
| 31 | + return; |
| 32 | + } |
| 33 | + base.HandleKeyOperations(args); |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +### VB |
| 39 | + |
| 40 | +``` vb |
| 41 | +Public Sub New() |
| 42 | + InitializeComponent() |
| 43 | + Me.sfDataGrid.SelectionController = New CustomSelectionController(sfDataGrid) |
| 44 | +End Sub |
| 45 | + |
| 46 | +Public Class CustomSelectionController Inherits Syncfusion.WinForms.DataGrid.Interactivity.RowSelectionController |
| 47 | + |
| 48 | + Private DataGrid As SfDataGrid |
| 49 | + |
| 50 | + Public Sub New(ByVal sfDataGrid As SfDataGrid) |
| 51 | + MyBase.New(sfDataGrid) |
| 52 | + Me.DataGrid = sfDataGrid |
| 53 | + End Sub |
| 54 | + |
| 55 | + Protected Overrides Sub HandleKeyOperations(ByVal args As KeyEventArgs) |
| 56 | + If args.KeyCode = Keys.Enter Then |
| 57 | + Dim arguments As New KeyEventArgs(Keys.Tab) |
| 58 | + MyBase.HandleKeyOperations(arguments) |
| 59 | + Return |
| 60 | + End If |
| 61 | + MyBase.HandleKeyOperations(args) |
| 62 | + End Sub |
| 63 | +End Class |
| 64 | +``` |
0 commit comments