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;
}
}
g sPosted May 8, 2015, 8:11 AM
http://dotnetdrizzles.blogspot.in/2015/04/datagirdview-row-color-and-alternate.html
http://screenshotdrizzles.blogspot.in/2015/04/datagirdview-row-color-and-alternate.html
Eline SamiPosted May 3, 2015, 11:59 PM
Yes. It is working!
I just modified the code to give the correct output.
if((row.cells[0].value).toString()=="High")
{
row.DefaultCellStyle.ForeColor = System.Drawing.Color.Red;
}
Isham KhanPosted May 3, 2015, 3:07 AM
if ((row.Cells[0].Value) =="high ")
{
row.DefaultCellStyle.BackColor = Color.Red;
}
else
{
row.DefaultCellStyle.BackColor = Color.Green;
}