Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Change individual DataGridView row colors based on column value

0.00/5 (No votes)
8 Jan 2010 1  
Another way to do something like this is to handle the RowPrePaint event and change the background there. The advantage to doing it this way is that if you have thousands of rows, you don't have to set it for every row at the beginning, which could take a few seconds. This way only sets it for...
Another way to do something like this is to handle the RowPrePaint event and change the background there. The advantage to doing it this way is that if you have thousands of rows, you don't have to set it for every row at the beginning, which could take a few seconds. This way only sets it for the rows currently visible. Also, since the color is based on a field in the row, changing that value causes the color to change.

Although I don't recall -- you may need to handle some change event to detect the change and call InvalidateRow or something like that to force it to repaint. Of course you could also just change the background color in the ValueChanged event handler.

Either way you do it, you can also change the background of individual cells.

Note: I'm not sure about the names of functions and event handlers in the above. I rely or MSDN and Intellisense and I'm too lazy to start Visual Studio to look them up ;)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here