How to change the foreground color of a cell in the datagridview

I am applying a forcolor (RED) to the grid cells conditionally for a datagridview within the CellFormatting event, however, after the datagrid is populated, I am trying to perform a logic to loop through all rows, and under certain condition I try to hide the value in certain cell of the row (by setting style.forecolor=WHITE.

The logic works OK except for the records that are targeted by the CellFormatting event that are changed to red color, the style.forecolor=WHITE does not apply. Is there a problem in changing the style forecolor after it is changed in the cellformatting?. Please help
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Can you show me your CellFormatting event handler, please?
Robert SchuttSoftware EngineerCommented:
The CellFormatting is called as the grid is being updated, so you when change the color to white, the grid is repainted, and your CellFormatting function changes the color back to red.

If you can, you should apply the coloring logic (that you are now doing after the grid is loaded) inside the CellFormatting function (or just do all of the formatting in the same function and forget about CellFormatting which might be even better because too much code in CellFormatting may slow the grid down significantly).

Otherwise for example if the red coloring is actually for a whole row you could use the rows DefaultCellStyle to set that to red and then the white cell style you set later will override that. But in general think about what you need to do and when because the CellFormatting function is called lots of times by the grid itself and may not be exactly what you think looking at it's name.

If you need the processing to stay as it is, as a last resort you could define an array to hold the cell colors, use that in CellFormatting and make sure you don't set any white cells to red yourself (but that may be a bit overkill).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fmichailAuthor Commented:
Dear Robert. Thank you for your detailed accurate, and definitely helpful answer.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.