Results 1 to 5 of 5

Thread: Change datagrid cell color based on the value of the cell?

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2016
    Posts
    4

    Change datagrid cell color based on the value of the cell?

    I've this DataGrid structure:

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

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

    Re: Change datagrid cell color based on the value of the cell?

    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:
    1. Private Sub SetColorCell()
    2.         BindUsingDataTable()
    3.  
    4.         For index = 0 To grid1.Items.Count - 1 Step 1
    5.             Dim firstRow As DataGridRow = grid1.GetRow(index)
    6.             Dim cell As Controls.DataGridCell = TryCast(grid1.Columns(2).GetCellContent(firstRow).Parent, Controls.DataGridCell)
    7.             'set background
    8.  
    9.             If TryCast(cell.Content, TextBlock).Text = "Dept1" Then
    10.                 cell.Background = Brushes.Green
    11.             End If
    12.         Next
    13.  
    14. End Sub

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

    Hope this will help.

    - kgc
    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...

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2016
    Posts
    4

    Re: Change datagrid cell color based on the value of the cell?

    Quote Originally Posted by KGComputers View Post
    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:
    1. Private Sub SetColorCell()
    2.         BindUsingDataTable()
    3.  
    4.         For index = 0 To grid1.Items.Count - 1 Step 1
    5.             Dim firstRow As DataGridRow = grid1.GetRow(index)
    6.             Dim cell As Controls.DataGridCell = TryCast(grid1.Columns(2).GetCellContent(firstRow).Parent, Controls.DataGridCell)
    7.             'set background
    8.  
    9.             If TryCast(cell.Content, TextBlock).Text = "Dept1" Then
    10.                 cell.Background = Brushes.Green
    11.             End If
    12.         Next
    13.  
    14. End Sub

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

    Hope this will help.

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

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

    Re: Change datagrid cell color based on the value of the cell?

    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
    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
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Change datagrid cell color based on the value of the cell?

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

    XAML Code:
    1. <DataGridTextColumn.CellStyle>
    2.                         <Style TargetType="DataGridCell">
    3.                             <Style.Triggers>
    4.                                 <DataTrigger Binding="{Binding Path=Age}" Value="50">
    5.                                     <Setter Property="Background" Value="Gray"/>
    6.                                 </DataTrigger>
    7.                             </Style.Triggers>
    8.                         </Style>
    9.                     </DataGridTextColumn.CellStyle>

    :-D
    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...

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