Skip to content

SyncfusionExamples/how-to-change-the-pageup-and-pagedown-key-behavior-in-winforms-data-grid

Repository files navigation

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

About the sample

This example illustrates how to change the PageUp and PageDown key behavior in WinForms DataGrid.

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 SfDataGrid

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

PageUp and PageDown key customization when Row selection applied in SfDataGrid

Take a moment to peruse the WinForms DataGrid – Selection documentation, where you can find about selection with code examples.

Requirements to run the demo

Visual Studio 2015 and above versions

About

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages