Skip to content

Commit d54e4f0

Browse files
ES-975464 - Committed the changes
1 parent a893db2 commit d54e4f0

File tree

1 file changed

+60
-4
lines changed

1 file changed

+60
-4
lines changed

README.md

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,63 @@
1-
# how-to-change-the-Enter-key-behavior-in-winforms-datagrid
1+
# How to Change the Enter Key Behavior in WinForms DataGrid?
22

3-
This example illustrates how to change the Enter key behavior in [WinForms DataGrid](https://www.syncfusion.com/winforms-ui-controls/datagrid).
3+
This example illustrates how to change the Enter key behavior in [WinForms DataGrid](https://www.syncfusion.com/winforms-ui-controls/datagrid) (SfDataGrid).
44

5-
When pressing the Enter key, the current cell will be moved to the next row in [DataGrid](https://www.syncfusion.com/winforms-ui-controls/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 SelectionController and overriding the [HandleKeyOperations](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGrid.Interactivity.SelectionControllerBase.html#Syncfusion_WinForms_DataGrid_Interactivity_SelectionControllerBase_HandleKeyOperations_System_Windows_Forms_KeyEventArgs_) method, which Handles selection when any of the Key operations such as Up, Down, Left, Right, Enter, etc., are performed in [DataGrid](https://www.syncfusion.com/winforms-ui-controls/datagrid).
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_).
66

7-
KB article: https://support.syncfusion.com/kb/article/8625/how-to-change-the-enter-key-behavior-in-winforms-datagrid-sfdatagrid
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+
Public Sub New(ByVal sfDataGrid As SfDataGrid)
50+
MyBase.New(sfDataGrid)
51+
Me.DataGrid = sfDataGrid
52+
End Sub
53+
54+
Protected Overrides Sub HandleKeyOperations(ByVal args As KeyEventArgs)
55+
If args.KeyCode = Keys.Enter Then
56+
Dim arguments As New KeyEventArgs(Keys.Tab)
57+
MyBase.HandleKeyOperations(arguments)
58+
Return
59+
End If
60+
MyBase.HandleKeyOperations(args)
61+
End Sub
62+
End Class
63+
```

0 commit comments

Comments
 (0)