# datagrid-conditional-styling

> SfDataGrid with a CellStyleSelector applying red, yellow, violet, and green styles by sales value.

**Framework:** winui  **Component:** DataGrid  **Variant:** conditional-styling

## Get this item

**If you are an agent, fetch the JSON.** Source is inlined, so one request is enough and no tooling is required:

```
GET https://ai.syncfusion.com/r/winui/datagrid-conditional-styling.json
```

**Install Package(s)**

```bash
dotnet add package Syncfusion.Grid.WinUI
```

**Notes**

- Syncfusion release: 2026 Volume 3 (v35.1.37)
- The Syncfusion package is licensed. The composition in this file is source you own and edit. See https://ai.syncfusion.com/licensing.md

## Source files

### src/components/datagrid-conditional-styling/DataGrid.xaml

```xaml
﻿<Page
    x:Class="DataGrid.Samples.ConditionalStyling.DataGrid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:DataGrid"
    xmlns:dataGrid="using:Syncfusion.UI.Xaml.DataGrid"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid x:Name="rootGrid">
        <Grid.Resources>
            <local:SalesViewModel x:Key="salesViewModel" />
            <local:CustomCellStyleSelector x:Key="cellStyleSelector" />

            <Style x:Key="darkRedCellStyle" TargetType="dataGrid:GridCell">
                <Setter Property="Background" Value="#f50602"/>
                <Setter Property="Foreground" Value="White"/>
            </Style>
            <Style x:Key="redCellStyle" TargetType="dataGrid:GridCell">
                <Setter Property="Background" Value="#e37b64"/>
                <Setter Property="Foreground" Value="Black"/>
            </Style>
            <Style x:Key="lightRedCellStyle" TargetType="dataGrid:GridCell">
                <Setter Property="Background" Value="#f07d9b"/>
                <Setter Property="Foreground" Value="Black"/>
            </Style>
            <Style x:Key="darkYellowCellStyle" TargetType="dataGrid:GridCell">
                <Setter Property="Background" Value="#fcca03"/>
                <Setter Property="Foreground" Value="White"/>
            </Style>
            <Style x:Key="yellowCellStyle" TargetType="dataGrid:GridCell">
                <Setter Property="Background" Value="#f0e090"/>
                <Setter Property="Foreground" Value="Black"/>
            </Style>
            <Style x:Key="lightYellowCellStyle" TargetType="dataGrid:GridCell">
                <Setter Property="Background" Value="#f5e6b5"/>
                <Setter Property="Foreground" Value="Black"/>
            </Style>
            <Style x:Key="darkVioletCellStyle" TargetType="dataGrid:GridCell">
                <Setter Property="Background" Value="#6f0e82"/>
                <Setter Property="Foreground" Value="White"/>
            </Style>
            <Style x:Key="violetCellStyle" TargetType="dataGrid:GridCell">
                <Setter Property="Background" Value="#b464c4"/>
                <Setter Property="Foreground" Value="Black"/>
            </Style>
            <Style x:Key="lightVioletCellStyle" TargetType="dataGrid:GridCell">
                <Setter Property="Background" Value="#e4b4ed"/>
                <Setter Property="Foreground" Value="Black"/>
            </Style>
            <Style x:Key="greenCellStyle" TargetType="dataGrid:GridCell">
                <Setter Property="Background" Value="#50bf65"/>
                <Setter Property="Foreground" Value="Black"/>
            </Style>
            <Style x:Key="lightGreenCellStyle" TargetType="dataGrid:GridCell">
                <Setter Property="Background" Value="#99f2b4"/>
                <Setter Property="Foreground" Value="Black"/>
            </Style>
        </Grid.Resources>

        <dataGrid:SfDataGrid
            x:Name="sfDataGrid"
            Height="450"
            Width="Auto"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Center"
            AutoGenerateColumns="False"
            ColumnWidthMode="Auto"
            CellStyleSelector="{StaticResource cellStyleSelector}"
            DataContext="{StaticResource salesViewModel}"
            ItemsSource="{Binding YearlySales}"
            NavigationMode="Row">

            <dataGrid:SfDataGrid.Columns>
                <dataGrid:GridTextColumn MappingName="Name" />
                <dataGrid:GridNumericColumn MappingName="QS1" NumberFormatter="{Binding SystemCurrency,Source={StaticResource salesViewModel}}" HeaderText="Sales in Q1" TextAlignment="Right" />
                <dataGrid:GridNumericColumn MappingName="QS2" NumberFormatter="{Binding SystemCurrency,Source={StaticResource salesViewModel}}" HeaderText="Sales in Q2" TextAlignment="Right" />
                <dataGrid:GridNumericColumn MappingName="QS3" NumberFormatter="{Binding SystemCurrency,Source={StaticResource salesViewModel}}" HeaderText="Sales in Q3" TextAlignment="Right" />
                <dataGrid:GridNumericColumn MappingName="QS4" NumberFormatter="{Binding SystemCurrency,Source={StaticResource salesViewModel}}" HeaderText="Sales in Q4" TextAlignment="Right" />
                <dataGrid:GridNumericColumn MappingName="Total" NumberFormatter="{Binding SystemCurrency,Source={StaticResource salesViewModel}}" HeaderText="Total Sales" TextAlignment="Right" />
                <dataGrid:GridNumericColumn MappingName="Year" HeaderText="Year" TextAlignment="Right" />
            </dataGrid:SfDataGrid.Columns>
        </dataGrid:SfDataGrid>
    </Grid>
</Page>
```
