Ticket Q455807
Visible to All Users

How to change the row color of the wpf grid programmatically

created 12 years ago

I want to change the row color of a wpf grid row based on the information bound to the grid. How would I do this in the C# code?

Answers approved by DevExpress Support

created 12 years ago (modified 12 years ago)
    Show previous comments (1)
    DevExpress Support Team 12 years ago

      Hi,
      It is necessary to change the background color of rows via the RowStyle property. It is not clear to me though how you wish to change the rows' color. Based on your scenario, I suggest you use either of the following two ways of doing this:

      1. To set one color for all rows based on a certain condition, set the same attached property to the GridControl and bind to this property in the row style.
      2. To set different colors for each row and make a particular color depend on row data, bind the row background color to certain row data in the RowStyle via a converter.
        If your scenario is different, please describe it in detail.
        Thanks

        The rows don't contain any data points which would specify the color of the row. Therefore I don't believe I can use any handy dandy wpf mechanisms. I will be calculating that in C# code but I am not sure of how to use your grid object model.
        For example.
        datagridcontrol.itemssource = items
        foreach (item in items)
        {
             calculate a value
             if value is greater than 0
             color row black
             else
             color row white
        }
        So when we set the color black or white, how would we do that line of code? Is it like gridview.row[x].Color = white?
        I simply want to specify the row color after my data is bound in the c# code behind.

        Alexander E (DevExpress) 12 years ago

          Hi Michael,
          Thank you for the clarification. You can do your calculation and checking in a ValueConverter that is used in the RowStyle's Background setter.
          I have attached a sample to demonstrate this. Please review it and let me know whether or not this approach meets your requirements.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.