# .NET and More > WPF, WCF, WF >  Change datagrid cell color based on the value of the cell?

## ShiroDin

I've this DataGrid structure:



```
<DataGrid ItemsSource="{Binding MatchService.Matches}" AutoGenerateColumns="False" 
                          CanUserAddRows="false" IsReadOnly="True">
        <DataGrid.Columns>
            <DataGridTextColumn Header="{DynamicResource championship}" Binding="{Binding Competition}"/>
            <DataGridTextColumn Header="1" Binding="{Binding HomeWin}"/>
            <DataGridTextColumn Header="X" Binding="{Binding Draw}"/>
        </DataGrid.Columns>

Essentially I need to change the background of all the cell that have 1 and X header, if the value of the row is: < 50 the cell should have a background of red, if is >60 green.

What I did until now is:

 <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <Trigger Property="" Value="50">

                </Trigger>
            </Style.Triggers>
        </Style>
```

But I doesn't found any property that bind the generic value each DataGridTextColumn.

How can I achieve this?

Thanks.

----------


## KGComputers

Hi,

I have a sample project here: Accessing Rows and Cells in a WPF DataGrid that will change the color of a cell using code instead of Triggers. Download the project and replace the SetColorCell() method with the snippet below.

This snippet will update the cell color in the third column if the value is Dept1.

vb.net Code:
Private Sub SetColorCell()        BindUsingDataTable()         For index = 0 To grid1.Items.Count - 1 Step 1            Dim firstRow As DataGridRow = grid1.GetRow(index)            Dim cell As Controls.DataGridCell = TryCast(grid1.Columns(2).GetCellContent(firstRow).Parent, Controls.DataGridCell)            'set background             If TryCast(cell.Content, TextBlock).Text = "Dept1" Then                cell.Background = Brushes.Green            End If        Next End Sub

Note: You need to include the DataGridExtensions module in your project with the GetRow() function only.

Hope this will help.

- kgc

----------


## ShiroDin

> Hi,
> 
> I have a sample project here: Accessing Rows and Cells in a WPF DataGrid that will change the color of a cell using code instead of Triggers. Download the project and replace the SetColorCell() method with the snippet below.
> 
> This snippet will update the cell color in the third column if the value is Dept1.
> 
> vb.net Code:
> Private Sub SetColorCell()
>         BindUsingDataTable()
> ...


Thanks for the answer, but I'm a c# developer and also I need a pure xaml solution..

----------


## KGComputers

> Thanks for the answer, but I'm a c# developer and also I need a pure xaml solution..


I dont' know if a solution can be achieved by pure XAML  alone. A little help from IValueConverter will achieve that. 

wpf-datagrid-trigger-on-cell-content

----------


## KGComputers

Ok, After doing some research, it can be done using DataGridTextColumn.CellStyle Trigger.


XAML Code:
<DataGridTextColumn.CellStyle>                        <Style TargetType="DataGridCell">                            <Style.Triggers>                                <DataTrigger Binding="{Binding Path=Age}" Value="50">                                    <Setter Property="Background" Value="Gray"/>                                </DataTrigger>                            </Style.Triggers>                        </Style>                    </DataGridTextColumn.CellStyle>

:-D

----------

