Eline Sami

Eline Sami

  • NA
  • 47
  • 68.5k

Change the fontcolor of a row in datagridview

May 3 2015 3:03 AM
Hi Vulupes

In my windows form applicaiton, I have a datagridview that contains several columns (not bound to a DB)
would like to change the fontcolor of the row during run time if the prioriry of the row is High or Medium

Red - if the priority of this row is high
Green - if the priority of this row is Medium

Example: if I have 10rows, (4 of them the priorirty is high and 2 of them the priority is medium)
So>> I have the following: 4rows the fontColor is Red, 2Rows the fontclor is green, the rest is default.

this is the code that I am trying to use, but if fails:

private void changeColor()
        {

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {

                //priority = row.Cells["Priority"].Value.ToString();
                switch (priority)
                {
                    //Change font
                    case "High":
                        row.DefaultCellStyle.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "Medium":
                        row.DefaultCellStyle.ForeColor = System.Drawing.Color.Green;
                        break;
                    case "Low":
                        row.DefaultCellStyle.ForeColor = System.Drawing.Color.Black;
                        break;
                    default:
                        row.DefaultCellStyle.ForeColor = System.Drawing.Color.Black;
                        break;
                }
            }


Answers (3)