How to set the ForeColor of a Cell in a DataGridView Control based on the Text Contained in an adjacent Cell in a Column to the left of it

FaheemAhmadGul
FaheemAhmadGul used Ask the Experts™
on
I have a DataGridViewControl named myGrid on a Form1 in a Visual Basic Project in Visual Studio 2008.

myGrid displays data from a database table in an SQL Server.
myGrid has two colums named Color and Comment.  Color is the first Column in myGrid and Comment is the 2nd Column.
Both colums display text data.
Cells in the first (Color) Colum can have one of the three values: Red, Yellow and Green.

I would like to set the ForeColor property of all the cells in Column two (ie Column named Comment) based on the text in the adjacent cell in the first (ie Color) Column.

If the text contained in a cell in the first column (ie Color) is Yellow, then I would like the ForeColor of the cell immediately to the right of it (ie in Column 2 named Comment) to be Yellow and if the text contained in a cell in the first column (ie Color) is Green, then I would like the ForeColor of the cell immediately to the right of it (ie in Column 2 named Comment) to be Green. Similarly if the Content of a Cell Color Column is Red then I would like the Forecolor of the cell immediately to the right of it to be Red.

I would appreciate help with code that can achieve this for me.

Many thanks for your help in anticipation.


myGridView.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I have edited my question slightly , added a diagram to make it more clear and increased the points to 400.
Top Expert 2010
Commented:
From the screen shot, it looks like you want to set BackColor, instead of ForeColor.
Try this after you have bound the data to the grid.
Note: If you want to set ForeColor, just change the BackColor to ForeColor in the code.

        For Each row As DataGridViewRow In DataGridView1.Rows
            Select Case row.Cells(0).FormattedValue.ToString.ToUpper
                Case "YELLOW"
                    row.Cells(1).Style.BackColor = Drawing.Color.Yellow
                Case "RED"
                    row.Cells(1).Style.BackColor = Drawing.Color.Red
                Case "GREEN"
                    row.Cells(1).Style.BackColor = Drawing.Color.Green
            End Select
        Next

Open in new window

Author

Commented:
Thank you for your response to my request for help. I put the code provided by you in the CellFormatting1 event of my DataGridView Control, but I got the error message with the following line from your code highlighted

Select Case row.Cells(0).FormattedValue.ToString.ToUpper

The details of the error were as follows.
An unhandled exception of type 'System.StackOverflowException' occured in System.Forms.dll
in the troublesthooting tips area the following line was underlined
Make sure you do not have an infinite loop or infinite recursion.

I would be grateful for some further help to correct this. Thanks

Author

Commented:
I have been able to correct the error by changing
Select Case row.Cells(0).FormattedValue.ToString.ToUpper

to
 Select Case row.Cells(0).Value

Many thanks for your help. I am very grateful. Regards

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial