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?
How to change the row color of the wpf grid programmatically
Answers approved by DevExpress Support
Please refer to the following examples, which should be helpful:
How to change background color for modified cells
A general approach to highlighting specific grid cells
How to implement conditional formatting for cells
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:
- 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.
- 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.
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.