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 the background for selected DetailsViewGrid in WPF DataGrid (SfDataGrid)?

Platform: WPF |
Control: SfDataGrid

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.

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