Datagrid view conditional formatting



Is there any way that conditional formatting can be applied to the Datagridview rows?

Being able to change the background/foreground color as well as the font type/sizes etc based on a field meeting a specific value or range of values would be really handy.  A typical use case would be a grid full of employee records, with one of the fields in the employees table called active.  If active is false, then the row background color would be a dark gray, giving a visual cue to the user that this record is different to the others.

Another use case would be were an integer value like a stock level falls in between a range of values, like stock remaining between 1 and 4, then setting the background color of the row to a red, giving that same visual cue to the user that more stock needs to be ordered, and if the stock level is between 5 and 10, then the background color of the row would be yellow and above 10 then the background row color would be whatever the default color is set at.

  • You must to post comments

There are several ways to achieve that.

One is to handle the CellFormatting event and change the Style passed in the event args. Another approach is to handle CellValueChanged and set the Style of the other cell. You can also cache and reuse DataGridViewCellStyle objects.

I have attached a small sample that shows both approaches since it answers another similar question.




  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.