I am working on a C# software linked to database with a datagridview. i want to switch the color of the last cell in every row. when the number is greater then 5 --> green if 5-->yellow and less then 5--> red. any help ?
Asked
Active
Viewed 54 times
-1
-
2https://stackoverflow.com/a/4067612/1271037 – dovid Aug 12 '17 at 18:05
-
Please edit your question to include the code showing what you've tried so far. – ddrjca Aug 12 '17 at 18:07
-
at which point in time do you wish this change to be executed? – Mong Zhu Aug 12 '17 at 18:07
-
in my code in can edit on the datagridview and update the database. so the number will be updated. and i want the colors to switch related to the number inside the cells – Edgard Mansour Aug 12 '17 at 18:17
1 Answers
0
You can try something like this:
public void Color()
{
foreach (DataGridViewRow row in ProductServicesDataGrid.Rows)
if (Convert.ToInt32(row.Cells[5].Value) > Convert.ToInt32(row.Cells[6].Value))
{
row.DefaultCellStyle.ForeColor = Color.DarkGreen;
}
else if (Convert.ToInt32(row.Cells[5].Value) < Convert.ToInt32(row.Cells[6].Value))
{
row.DefaultCellStyle.ForeColor = Color.Yellow;
}
else if (Convert.ToInt32(row.Cells[5].Value) == 0 && Convert.ToInt32(row.Cells[6].Value) > Convert.ToInt32(row.Cells[5].Value))
{
row.DefaultCellStyle.ForeColor = Color.Red;
}
else
{
row.DefaultCellStyle.ForeColor = Color.DarkBlue;
}
}

Hammad Ahmed
- 60
- 2
- 9