Colorize line in datagridview c#

Asked

Viewed 1,460 times

0

I’m trying to use the following method:

  for (int i = 0; i < dgvRequisicao.Rows.Count; i++)
        {
            valor = Convert.ToDouble(dgvRequisicao.Rows[i].Cells["valor"].Value);

            if (valor > 20000.01 && valor < 100000)
                dgvRequisicao.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
            else if (valor > 100000.01)
                dgvRequisicao.Rows[i].DefaultCellStyle.BackColor = Color.Red;
        }

But you’re not coloring at all.

Thank you

1 answer

1

You will need to use the event CellFormating.

private void dgvRequisicao_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    valor = Convert.ToDouble((string)e.Value);

    if (valor > 20000.01 && valor < 100000)
        dgvRequisicao.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
    else if (valor > 100000.01)
        dgvRequisicao.Rows[i].DefaultCellStyle.BackColor = Color.Red;
}
  • It works. But it takes me a while to load everything

Browser other questions tagged

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