How to change the PageUp and PageDown key behavior in WinForms DataGrid (SfDataGrid)?
By default, in WinForms DataGrid (SfDataGrid), if PageUp or PageDown key is pressed, then the DataGrid will be scrolled to the previous set of rows or the next set of rows that are not displayed in the view.
You can change this behavior like Tab key navigation that moves the current cell to next cell of the same row by overriding HandleKeyOperations method in CellSelectionController / RowSelectionController class and setting it to SfDataGrid.SelectionController property. When SelectionUnit is Cell, you have to override CellSelectionController and when SelectionUnit is Row, you have to override RowSelectionController class.
Cell Selection
// set the customized CustomCellSelectionController to SfDataGrid.SelectionController when CellSelection applied in SfDataGrid sfDataGrid.SelectionUnit = SelectionUnit.Cell; sfDataGrid.SelectionController = new CustomCellSelectionController(this.sfDataGrid); //Inherits the CustomCellSelectionController Class public class CustomCellSelectionController : CellSelectionController { public CustomCellSelectionController(SfDataGrid dataGrid) : base(dataGrid) { } //overriding the HandleKeyOperations Event from CellSelectionController base class protected override void HandleKeyOperations(KeyEventArgs args) { //Key based Customization if (args.KeyCode == Keys.PageUp || args.KeyCode == Keys.PageDown) { //assigning the state of Tab key Event handling to PageUp and PageDown key KeyEventArgs arguments = new KeyEventArgs(Keys.Tab); base.HandleKeyOperations(arguments); return; } base.HandleKeyOperations(args); } }
Row Selection
// set the customized CustomSelectionController to SfDataGrid.SelectionController when RowSelection applied in SfDataGrid sfDataGrid.SelectionUnit = SelectionUnit.Row; sfDataGrid.SelectionController = new CustomSelectionController(this.sfDataGrid); //Inherits the CustomSelectionController Class public class CustomSelectionController : RowSelectionController { public CustomSelectionController(SfDataGrid dataGrid) : base(dataGrid) { } //overriding the HandleKeyOperations Event from RowSelectionController base class protected override void HandleKeyOperations(KeyEventArgs args) { //Key based Customization if (args.KeyCode == Keys.PageUp || args.KeyCode == Keys.PageDown) { //assigning the state of Tab key Event handling to PageUp and PageDown key KeyEventArgs arguments = new KeyEventArgs(Keys.Tab); base.HandleKeyOperations(arguments); return; } base.HandleKeyOperations(args); } }
Take a moment to peruse the WinForms DataGrid – Selection documentation, where you can find about selection with code examples.