How to move the CurrentCell to the first column of the AddNewRow when the Tab key is pressed from the last column and its position is at the Bottom of the SfDataGrid?
In the SfDataGrid, when the Tab Key is pressed from the last column of the AddNewRow, the values are committed and the CurrentCell remains in the last column of the AddNewRow. To change this behavior and move the CurrentCell to the first column of the AddNewRow, change the Tab key’s behavior by overriding the GridSelectionController class and setting it to the SfDataGrid.SelectionController property.
The following code example shows how to set the instance of overriding the GridSelectionController class to the SfDataGird.SelectionController property.
C#
public MainWindow() { InitializeComponent(); this.sfdatagrid.SelectionController = new CustomSelectionController(sfdatagrid); }
The following code example explains how to override the GridCellSelectionController class. Here, you need to override the ProcessKeyDown method. Within this method, you can move the CurrentCell to the first column of the AddNewRow by using the GridSelectionController.MoveCurrentCell method.
C#
public class CustomSelectionController : GridSelectionController { public CustomSelectionController(SfDataGrid dataGrid) : base(dataGrid) { } protected override void ProcessKeyDown(KeyEventArgs args) { // Get the CurrentCell column index var currentColumnIndex = this.CurrentCellManager.CurrentRowColumnIndex.ColumnIndex; // Get last column index of the SfDataGrid through the SelectionHelper var lastIndex = SelectionHelper.GetLastColumnIndex(DataGrid); base.ProcessKeyDown(args); bool isAddNewRow = DataGrid.IsAddNewIndex(CurrentCellManager.CurrentRowColumnIndex.RowIndex); if (SelectionHelper.CheckShiftKeyPressed()) return; if (args.Key == Key.Tab && isAddNewRow && this.DataGrid.AddNewRowPosition == AddNewRowPosition.Bottom) { var rowIndex = this.CurrentCellManager.CurrentRowColumnIndex.RowIndex; var firstFocusedColumnIndex = GetFirstFocusedColumnIndex(); var columnIndex = currentColumnIndex == lastIndex ? GetNextFocusedColumnIndex(0) : GetNextFocusedColumnIndex(currentColumnIndex); if (columnIndex == firstFocusedColumnIndex && this.DataGrid.View.IsAddingNew) { this.CommitAddNew(); rowIndex = (GridHelper.GetAddNewRowController(this.DataGrid) as GridAddNewRowController).GetAddNewRowIndex(); } this.MoveCurrentCell(new RowColumnIndex(rowIndex,columnIndex)); this.DataGrid.ScrollInView(this.CurrentCellManager.CurrentRowColumnIndex); } } //Get the First Focused Column Index private int GetFirstFocusedColumnIndex() { int temp = 0; var dataColumn = SelectionHelper.GetDataColumnBase(this.DataGrid, new RowColumnIndex(this.CurrentCellManager.CurrentRowColumnIndex.RowIndex, temp)); while (dataColumn != null && (!dataColumn.GridColumn.AllowFocus || dataColumn.GridColumn.IsHidden)) { temp++; dataColumn = SelectionHelper.GetDataColumnBase(this.DataGrid, new RowColumnIndex(this.CurrentCellManager.CurrentRowColumnIndex.RowIndex, temp)); if (dataColumn == null) { temp = 0; dataColumn = SelectionHelper.GetDataColumnBase(this.DataGrid, new RowColumnIndex(this.CurrentCellManager.CurrentRowColumnIndex.RowIndex, temp)); } } return this.DataGrid.ResolveToScrollColumnIndex(temp); } //Get the Next Focused Index when Move to new line public int GetNextFocusedColumnIndex(int index) { int temp = 0; temp = index + 1; var dataColumn = SelectionHelper.GetDataColumnBase(this.DataGrid, new RowColumnIndex(this.CurrentCellManager.CurrentRowColumnIndex.RowIndex, temp)); while (dataColumn != null && (!dataColumn.GridColumn.AllowFocus || dataColumn.GridColumn.IsHidden)) { temp++; dataColumn = SelectionHelper.GetDataColumnBase(this.DataGrid, new RowColumnIndex(this.CurrentCellManager.CurrentRowColumnIndex.RowIndex, temp)); if (dataColumn == null) { temp = 0; dataColumn = SelectionHelper.GetDataColumnBase(this.DataGrid, new RowColumnIndex(this.CurrentCellManager.CurrentRowColumnIndex.RowIndex, temp)); } } return this.DataGrid.ResolveToScrollColumnIndex(temp); } } }
You need to override the GridCellSelectionController class when the SelectionUnit is the GridSelectionUnit.Cell.
Sample Links: