-1

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 ?

dovid
  • 6,354
  • 3
  • 33
  • 73

1 Answers1

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