Articles in this section
Category / Section

How to select the rows based on a cell value in WPF DataGrid (SfDataGrid)?

2 mins read

You can select the rows based on cell value by adding corresponding records to SelectedItems in WPF DataGrid (SfDataGrid). You can get the cell value of the particular cell using View.GetPropertyAccessprovider method like below code example.

C#

private void Button_Click(object sender, RoutedEventArgs e)
{
    if (datagrid.View != null)
        reflector = datagrid.View.GetPropertyAccessProvider();
    else
        reflector = null;
 
    var totalRowIndex = datagrid.View.Records.Count;
    var totalColumnIndex = datagrid.Columns.Count;
 
    for (int recordIndex = 0; recordIndex < totalRowIndex; recordIndex++)
    {
        for (int colindex = 0; colindex < totalColumnIndex; colindex++)
        {
            var record = this.datagrid.View.Records[recordIndex];
            var mappingName = datagrid.Columns[colindex].MappingName;
            //Get the cell value based on mappingName.
            var currentCellValue = reflector.GetValue(record.Data, mappingName);
            if (currentCellValue == "Bulk")
            {
                object item = datagrid.View.Records[recordIndex];
                //selected rows should be added here.
                datagrid.SelectedItems.Add(item);
            }
        }
    }
}

View sample in GitHub.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied