Change font color for a Datagridview column

Asked

Viewed 23 times

1

I’m trying to change the font color only give cell of a column that contains "x" value... but I’m only able to change the whole row... I tried to make only cell but without success, follow code used so far.

foreach (DataGridViewRow row in tableLeitura.Rows)
{

    if (Convert.ToInt32(row.Cells["GLicemiaCalc"].Value) > 180)
    {
         // Se celula for menor que 70
         row.DefaultCellStyle.ForeColor = Color.DeepSkyBlue;
    }
}

1 answer

1


Basically you need to develop a routine that interacts about each row (Datagridviewrow) contained in the Datagridview and test the value of a given cell and with the result then test the value returned if it satisfies the value 180, example:

private void Color_DataGridView()
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
                       //row.Cells["GLicemiaCalc"]
        if (int.TryParse(row.Cells[1].Value.ToString(), out int value))
        {
            if (value > 180)
            {
                row.Cells[1].Style.BackColor = Color.DeepSkyBlue;
            }
        }
    }
}
  • Thank you very much, I got it here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.