Articles in this section
Category / Section

How to set the background for selected DetailsViewGrid in WPF DataGrid (SfDataGrid)?

1 min read

You can set the background color of DetailsViewDataGrid by using the DetailsViewDataGrid.Background property in WPF DataGrid (SfDataGrid). This property can be used by retrieving the selected DetailsViewDataGrid using the GetDetailsViewGrid method by passing row index of the selected details view record. To set the background color of DetailsViewRowControl, return any one color by retrieving the selected DetailsViewRowControl using the converter method. You can get a row index of the record by using the ResolveToRowIndex extension method present in Syncfusion.UI.Xaml.Grid.Helpers.

Refer to the sample for changing the background color of DetailsViewDataGrid and DetailsViewRowControl using the converter and SfDataGrid.SelectionChanged events.

C#:

private void DataGrid_SelectionChanged(object sender, Syncfusion.UI.Xaml.Grid.GridSelectionChangedEventArgs e)
{
    if (e.RemovedItems.Count > 0)
    {
        var rowIndex = (e.RemovedItems[0] as GridRowInfo).RowIndex;
        var detailsViewDataGrid = this.dataGrid.GetDetailsViewGrid(this.dataGrid.ResolveToRecordIndex(rowIndex), "OrderDetails");
        if (detailsViewDataGrid == null)
            return;
        detailsViewDataGrid.Background = Brushes.LightBlue;
    }
    if (e.AddedItems.Count > 0)
    {
        var rowIndex = (e.AddedItems[0] as GridRowInfo).RowIndex;
        var detailsViewDataGrid = this.dataGrid.GetDetailsViewGrid(this.dataGrid.ResolveToRecordIndex(rowIndex), "OrderDetails");
        if (detailsViewDataGrid == null)
            return;
        detailsViewDataGrid.Background = Brushes.LightGreen;
    }
}    
 
public class BackGroundConverter: IValueConverter
{ 
 
    public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return "DarkGray";
    }
 
    public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

 

XAML:

<syncfusion:SfDataGrid.Resources>
     <Style TargetType="syncfusion:DetailsViewRowControl">
         <Setter Property="Background" Value="{Binding Path=SelectedItem, ElementName=dataGrid,Converter={StaticResource BackGroundConverter}}"></Setter>
     </Style>
     <Style TargetType="syncfusion:DetailsViewDataGrid">
         <Setter Property="Background" Value="White"></Setter>
     </Style>
 </syncfusion:SfDataGrid.Resources>
 

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 sign in to leave a comment
Access denied
Access denied