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
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to set current cell on particular row when DataGrid loaded?

Platform: WPF |
Control: SfDataGrid |
Published Date: August 12, 2014 |
Last Revised Date: July 27, 2021 |
.NET Framework: 4.5 |
Product Version: 12.1.0.43

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:

WPF

WRT

SilverLight

UWP

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile