# .NET and More > WPF, WCF, WF >  [RESOLVED] Changing background color for Datagrid Rows at run time

## tonyallan8128

Hello.
Ok the situation is . 
I have a Datagrid populated from a Datatable.
Every second row has a back ground color (AlternationCount="2") just to make the grid easier to read.
Any row that has a 1 in the State column I highlight with red.
Any row that has a 2 in the State column I highlight with green.
The problem is only the rows that have not been highlighted with the AlternationCount="2" change.
How do I keep the alternating rows and still change the rows with State 1 and 2 to their colors?

Thank you .



```

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DataGridRowColourChange"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
    <Grid Initialized="Grid_Initialized">
        <DataGrid x:Name="dataGrid1" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Height="346" RowHeaderWidth="0" AutoGenerateColumns="False" CanUserAddRows="False" CanUserResizeColumns="False" CanUserDeleteRows="False" ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Visible" AlternationCount="2"   HeadersVisibility="Column"  IsReadOnly="True" >
            <DataGrid.RowBackground>
                <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlLightColorKey}}"/>
            </DataGrid.RowBackground>
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding State}" Value="1">
                            <Setter Property="Background" Value="Red"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding State}" Value="2">
                            <Setter Property="Background" Value="Green"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Seq" Binding="{Binding Seq}" Width="50*" />
                <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" Width="120*" />
                <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" Width="120*" />
                <DataGridTextColumn Header="State" Binding="{Binding State}" Width="120*" />
            </DataGrid.Columns>


        </DataGrid>
    </Grid>
</Window>
```




```
Imports System.Data

Class MainWindow



    Private Sub Grid_Initialized(sender As Object, e As EventArgs)
        'Build Data Set
        Dim dsMain = New DataSet("Data for Data Grid1")

        ' Build the Students table.
        Dim dtDetails As New DataTable("Details")
        dsMain.Tables.Add(dtDetails)

        'Add Columns to Table Details
        dtDetails.Columns.Add("Seq", GetType(Integer))
        dtDetails.Columns.Add("FirstName", GetType(String))
        dtDetails.Columns.Add("LastName", GetType(String))
        dtDetails.Columns.Add("State", GetType(Integer))

        ' Populate the Details table.
        Dim Details_data(3) As Object
        Details_data(0) = 1
        Details_data(1) = "Amy"
        Details_data(2) = "Jackson"
        Details_data(3) = 1
        dtDetails.Rows.Add(Details_data)

        Details_data(0) = 1
        Details_data(1) = "Amy"
        Details_data(2) = "Jackson"
        Details_data(3) = 1
        dtDetails.Rows.Add(Details_data)

        Details_data(0) = 1
        Details_data(1) = "Amy"
        Details_data(2) = "Jackson"
        Details_data(3) = 1
        dtDetails.Rows.Add(Details_data)

        Details_data(0) = 1
        Details_data(1) = "Amy"
        Details_data(2) = "Jackson"
        Details_data(3) = 1
        dtDetails.Rows.Add(Details_data)

        Details_data(0) = 1
        Details_data(1) = "Amy"
        Details_data(2) = "Jackson"
        Details_data(3) = 1
        dtDetails.Rows.Add(Details_data)

        Details_data(0) = 1
        Details_data(1) = "Amy"
        Details_data(2) = "Jackson"
        Details_data(3) = 1
        dtDetails.Rows.Add(Details_data)

        Details_data(0) = 1
        Details_data(1) = "Amy"
        Details_data(2) = "Jackson"
        Details_data(3) = 1
        dtDetails.Rows.Add(Details_data)

        Details_data(0) = 1
        Details_data(1) = "Amy"
        Details_data(2) = "Jackson"
        Details_data(3) = 2
        dtDetails.Rows.Add(Details_data)

        Details_data(0) = 1
        Details_data(1) = "Amy"
        Details_data(2) = "Jackson"
        Details_data(3) = 2
        dtDetails.Rows.Add(Details_data)


        dataGrid1.ItemsSource = dtDetails.DefaultView


    End Sub
End Class
```

----------


## KenCash

Sounds like your alternating row is overriding the row style.

Try this...


```
<DataGrid.Style>
    <Style TargetType="DataGrid">
        <Setter Property="AlternatingRowBackground" Value="Blue"/>
    </Style>
</DataGrid.Style>
```

----------


## Shaggy Hiker

Thread moved to WPF, since that's what the code appears to be.

----------


## KGComputers

Remove AlternationCount=2 declared in the DataGrid element and transfer it to DataGrid.Style. Remove DataGrid.RowBackground XAML too but retain DataGrid.RowStyle.


XAML Code:
<DataGrid.Style>                                <Style TargetType="{x:Type DataGrid}">                    <Setter Property="RowBackground" Value="{StaticResource ConrolLightColorKey}"/>                    <Setter Property="AlternationCount" Value="2" />                </Style>            </DataGrid.Style>

Note: where ConrolLightColorKey is a SolidColorBrush resource. 


XAML Code:
<SolidColorBrush x:Key="ConrolLightColorKey" Color="{DynamicResource {x:Static SystemColors.ControlLightColorKey}}"/>

----------


## tonyallan8128

Thank you Ken Cash and KGComputers I tried both solutions and both did the job. 
Have a top day.

Tony

----------

