New to Telerik UI for WinFormsStart a free 30-day trial

How to Move the Current Row with Arrow Keys while RadGridView is in Edit Mode

Updated over 6 months ago

Environment

Product VersionProductAuthor
2023.2.718RadGridView for WinFormsDesislava Yordanova

Description

When the editor in RadGridView is active, it is expected to handle the user's input. For example, when a spin editor is activated in a decimal column, pressing the up/down arrow keys increases/decreases the editor's value. If you want to skip handling the up/down arrows by the editor and move the current row in the respective direction, you can have a look at the following code snippet which result is illustrated below:

move-current-row-with-arrow-keys-in-edit-mode

Solution

When the editor is initialized, we can handle the keyboard processing and execute custom logic when the up/down arrow keys are pressed. In addition, the default logic for increasing/decreasing the value in the spin editor should be restricted:

C#

        public RadForm1()
        {
            InitializeComponent();
            this.radGridView1.CellEditorInitialized += RadGridView_CellEditorInitialized;
        }

        private void RadGridView_CellEditorInitialized(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            var spinEditor = e.ActiveEditor as GridSpinEditor;
            if (spinEditor != null)
            {
                var element = spinEditor.EditorElement as RadSpinEditorElement;
                element.InterceptArrowKeys = false;
                element.KeyDown -= element_KeyDown;
                element.KeyDown += element_KeyDown;
            }
        }

        private void element_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Down)
                this.radGridView1.GridNavigator.SelectNextRow(1);
            else if (e.KeyData == Keys.Up)
                this.radGridView1.GridNavigator.SelectPreviousRow(1);
        }
        

See Also