Category / Section
How to set current cell on particular row when DataGrid loaded?
1 min read
SfDataGrid is loaded without any row or cell selection. When SelectionUnit property is set to Row then the row can be selected by setting SelectedIndex or SelectedItem property.
You can select the CurrentCell by invoking SfDataGrid.MoveCurrentCell method.
MoveCurrentCell method accepts two arguments as follows,
- RowColumnIndex – It receives CurrentCell of RowIndex and ColumnIndex.
- NeedToClearSelection – It’s optional parameter and Boolean type. When the parameter is ‘true’, clear all the selection in view and then move to CurrentCell when SelectionMode property is set to Extended. When it is ‘false’ select only the CurrentCell.
C#
this.dataGrid.Loaded += dataGrid_Loaded; void dataGrid_Loaded(object sender, RoutedEventArgs e) { var viewModel = this.dataGrid.DataContext as ViewModel; //find the RowIndex for particular record var RowIndex = this.dataGrid.ResolveToRowIndex(viewModel.OrdersDetails[5]); // find the ColumnIndex for that row. var ColumnIndex = this.dataGrid.ResolveToScrollColumnIndex(3); // CurrentCell is set if MappingName is EmployeeID if (this.dataGrid.Columns[ColumnIndex].MappingName == "EmployeeID") this.dataGrid.MoveCurrentCell(new RowColumnIndex(RowIndex, ColumnIndex)); }
For Silverlight:
In Silverlight, move current cell selection is achieved by handling ItemsSourceChanged.
Refer the following code example.
C#
this.dataGrid.ItemsSourceChanged += dataGrid_ItemsSourceChanged; void dataGrid_ItemsSourceChanged(object sender, GridItemsSourceChangedEventArgs e) { var viewModel = this.dataGrid.DataContext as ViewModel; //find the RowIndex for particular int RowIndex = this.dataGrid.ResolveToRowIndex(viewModel.OrdersDetails[5]); //find the ColumnIndex for that row. var ColumnIndex = this.dataGrid.ResolveToScrollColumnIndex(3); // CurrentCell is set if MappingName is EmployeeID if (this.dataGrid.Columns[ColumnIndex].MappingName == "EmployeeID") this.dataGrid.MoveCurrentCell(new RowColumnIndex(RowIndex, ColumnIndex)); }
Here, the CurrentCell is selected based on RowIndex of 5th record in OrdersDetails collection and 4th ColumnIndex by using MoveCurrentCell method.
Sample Links: