Articles in this section
Category / Section

How to change the cell background based on the cell value in WPF DataGrid (SfDataGrid)?

1 min read

WPF DatGrid (SfDataGrid) record rows (VirtualizingCellsControl ) can be customized conditionally by changing its property value based on ‘cell value’ or ‘data object’ by using converter, where converter returns the value based on Underlying record.

Here, rows background is changed using converter, where converter returns the color value based on Colors property of underlying record.

<Window.Resources>
        <local:ColorConverter x:Key="converter"/>
        <Style TargetType="syncfusion:VirtualizingCellsControl">
            <Setter Property="Background" Value="{Binding Converter={StaticResource converter}}" />
        </Style>
</Window.Resources>

 

public class ColorConverter : IValueConverter
{
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //Get the DataRowView
            var dataRow = value as DataRowView;
 
            //Get the colors name value
            var record = dataRow.Row["Colors"].ToString();
 
            //You can use a BrushConverter
            SolidColorBrush redBrush = (SolidColorBrush)new BrushConverter().ConvertFromString(record);
 
            //return the color based on DataBase cell value
            return redBrush;
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
}

Shows the changing cell background based on cell value in SfDataGridThe following screenshot shows the changed cell background based on the cell value,Shows the cell background based on the cell value in SfDataGridTake a moment to peruse the WPF DataGrid - Conditional Styling documentation, where you can find about conditional styling with code examples.

You can download the example from 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