Results 1 to 5 of 5

Thread: [RESOLVED] Changing background color for Datagrid Rows at run time

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2013
    Posts
    66

    Resolved [RESOLVED] Changing background color for Datagrid Rows at run time

    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 .

    Code:
    <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>

    Code:
    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

  2. #2
    New Member
    Join Date
    Nov 2016
    Posts
    3

    Re: Changing background color for Datagrid Rows at run time

    Sounds like your alternating row is overriding the row style.

    Try this...
    Code:
    <DataGrid.Style>
        <Style TargetType="DataGrid">
            <Setter Property="AlternatingRowBackground" Value="Blue"/>
        </Style>
    </DataGrid.Style>

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Changing background color for Datagrid Rows at run time

    Thread moved to WPF, since that's what the code appears to be.
    My usual boring signature: Nothing

  4. #4
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Changing background color for Datagrid Rows at run time

    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:
    1. <DataGrid.Style>                
    2.                 <Style TargetType="{x:Type DataGrid}">
    3.                     <Setter Property="RowBackground" Value="{StaticResource ConrolLightColorKey}"/>
    4.                     <Setter Property="AlternationCount" Value="2" />
    5.                 </Style>
    6.             </DataGrid.Style>

    Note: where ConrolLightColorKey is a SolidColorBrush resource.

    XAML Code:
    1. <SolidColorBrush x:Key="ConrolLightColorKey" Color="{DynamicResource {x:Static SystemColors.ControlLightColorKey}}"/>
    Last edited by KGComputers; Nov 4th, 2016 at 09:46 AM.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2013
    Posts
    66

    Re: Changing background color for Datagrid Rows at run time

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

    Tony

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width