Articles in this section
Category / Section

How to change the PageUp and PageDown key behavior in WinForms DataGrid (SfDataGrid)?

2 mins read

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);
        }
}

PageUp and PageDown key customization when Cell selection applied in SfDataGridRow 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);
        }
}

PageUp and PageDown key customization when Row selection applied in SfDataGridTake a moment to peruse the WinForms DataGrid – Selection documentation, where you can find about selection with code examples.

View Sample in GitHub

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied