Welcome to the Xceed Community | Help
Community Search  
More Search Options

How Can I Set the Background Color of Multiple Cells in a Row based on the Value of another Cell in the Same Row?

Sort Posts: Previous Next
  •  04-09-2012, 5:08 PM Post no. 31964

    How Can I Set the Background Color of Multiple Cells in a Row based on the Value of another Cell in the Same Row?

    Hello.  How can I set the background color of multiple cells in a row based on the value of a separate cell in the same row?  I figured out a round-about way to set the background of one cell in a row based on another cell in a row, but my solution doesn't work for setting multiple cells in a row.  Here is my solution:

                     <xcdg:Column FieldName="." Title="Sales">

                        <xcdg:Column.CellContentTemplate>

                            <DataTemplate>

                                <TextBlock x:Name="txtSales" Text="{Binding Path=Sales}"/>

                                <DataTemplate.Triggers>

                                    <DataTrigger Binding="{Binding IsTop}" Value="True">

                                        <Setter TargetName="txtSales" Property="Background" Value="Yellow"/>

                                    </DataTrigger>

                                </DataTemplate.Triggers>

                            </DataTemplate>

                        </xcdg:Column.CellContentTemplate>

                    </xcdg:Column>

    I have a column called "Sales".  I want the background of my "Sales" column to be yellow if the value of "IsTop" is true.  This code works.  It's not an ideal solution* but it works in this case

    My problem is that this solution doesn't work if I try to use it with multiple columns.  If I add another column with a data template similar to what I have above, I will get an exception that says that I can't add multiple columns to the column collection with the same FieldName.  My solution relies on me being able to set the column's FieldName to a period (.).  I can't have multiple columns with FieldNames set to a period (.).

    Is there a better way to set the background color of a cell based on the value of another cell in the same row?  Can I somehow use a binding to get the parent row of a cell?  I tried using the Ancestor syntax, but I can't figure out how to get the code to recognize that the parent object is a DataRowView.  I naively tried to set a binding path to "../IsTop" hoping that the binding uses a Unix-like syntax, but that didn't work.  Thanks.

     

    * The reason I say that this solution is not ideal is that it breaks the copy-and-paste action.  If I select the whole grid and copy-and-paste it into Notepad, all of the values of my "Sales" column will be "System.Data.DataRowView" rather than the actual value of the Sales column in that row.  I'm binding my datagrid to a System.Data.DataTable.

  •  04-09-2012, 5:13 PM Post no. 31965 in reply to 31964

    Re: How Can I Set the Background Color of Multiple Cells in a Row based on the Value of another Cell in the Same Row?

    Attachment: ScrollReset.zip

    I put together a solution that exhibits this problem.  Please the attached "ScrollReset.zip" file.  The solution, as-is, shows how I highlight a single cell in a row based on the value of another cell.

    If you un-comment the code block in the XAML file, you'll see how I attempted to set the background color of a second cell based on the same "IsTop" field.  If you un-comment that code and try to run the project, you'll get a runtime exception.

  •  04-10-2012, 4:56 PM Post no. 31970 in reply to 31965

    Re: How Can I Set the Background Color of Multiple Cells in a Row based on the Value of another Cell in the Same Row?

    Hi Jordan,

    The FieldName must be unique, so the error you are getting is normal.

    In regards to how to set the background color under specific conditions, I would strongly recommend the following blog post as it shows a few ways to do this:

    http://xceed.com/CS/blogs/techside/archive/2011/07/06/datacell-styling-vs-cellcontenttemplate.aspx

     


    ** Quick Tip: Clients with an active support subscription should be sending their questions by email if they wish to benefit from the faster response time. Thanks!


    Diane Lafontaine
    Technical Support
    Xceed Software Inc.
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.