Click here to Skip to main content
15,886,693 members
Articles / Desktop Programming / Windows Forms
Alternative
Tip/Trick

Change individual DataGridView row colors based on column value

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
8 Jan 2010CPOL 9.9K   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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --