We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to replace the behavior of editing value of the GridDateTimeColumn when pressing arrow up/down by navigate to the next cell

Hello,

The title explain it all, I would like to navigate to the next cell of the datagrid when I press the up or down arrow instead of increase the value of the date in the GridDateTimeColumn.

The enter key do the jobs but we would like the same behavior for the arrow up/down.

Thank you

Image_2190_1695375460126





Attachment: SfDataGridDemo_NET7_2c74923a.rar

3 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team September 25, 2023 03:00 PM UTC

Hi Nicolas Wagener,

Your requirement to navigate to the next cell of the SfDataGrid when pressing the up or down arrow instead of increasing the value of the date in the GridDateTimeColumn can be achieved by using the MoveCurrentCell helper method and customizing the PreviewKeyDown event. Refer to the below code snippet,


// Event subscription

sfDataGrid.PreviewKeyDown += OnPreviewKeyDown;

 

//Event customization

 private void OnPreviewKeyDown(object sender, KeyEventArgs e)

 {

     // Check if the key pressed is Up or Down arrow key

     if (e.Key == Key.Up|| e.Key == Key.Down)

     {              

         // Get the current cell

         DataColumnBase currentCell = sfDataGrid.SelectionController?.CurrentCellManager?.CurrentCell;

 

         // Check if the current cell is not null

         if (currentCell != null)

         {

             // Get the current cell row index

             int currentRowIndex = currentCell.RowIndex;

             // Get the current cell column index

             int currentColumnIndex = currentCell.ColumnIndex;

 

             // Check if the current cell is DateTime column

             if (sfDataGrid.Columns[currentColumnIndex].CellType == "DateTime")

             {

                 // End the edit when the current cell is first or last row

                 if (currentRowIndex == sfDataGrid.GetFirstRowIndex() || currentRowIndex == sfDataGrid.GetLastRowIndex())

                     this.sfDataGrid.SelectionController.CurrentCellManager.EndEdit();

                 else

                 // Move focus to the next cell

                 if (e.Key == Key.Up && currentRowIndex > sfDataGrid.GetFirstRowIndex())

                 {

                     // Move focus to the previous cell

                     sfDataGrid.MoveCurrentCell(new RowColumnIndex(currentRowIndex - 1, currentColumnIndex));

                 }

                 else if (e.Key == Key.Down && currentRowIndex < sfDataGrid.GetLastRowIndex())

                 {

                     // Move focus to the next cell

                     sfDataGrid.MoveCurrentCell(new RowColumnIndex(currentRowIndex + 1, currentColumnIndex));

                 }

             }

         }

     }

 }


UG Link:
Editing in WPF DataGrid control | Syncfusion

Selection in WPF DataGrid control | Syncfusion

Find the modified sample demo in the attachment.

Regards,
Vijayarasan S

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: Modified_Sample_afbe8e5b.zip

Marked as answer

NW Nicolas Wagener September 26, 2023 04:53 AM UTC

Hello Vijayarasan,


Thank you it's a perfect trick.


Have a nice day



VS Vijayarasan Sivanandham Syncfusion Team September 26, 2023 02:09 PM UTC

Nicolas Wagener,

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you
😊.


Loader.
Live Chat Icon For mobile
Up arrow icon