monteirobrena
monteirobrena

Reputation: 2620

Why the first line doesn't update ForeColor property?

I've tried alter ForeColor of all rows of my DataGridView using this code:

private void dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
   if (e.RowIndex >= 0) {
      DataGridViewRow row = dgv.Rows[e.RowIndex];
      e.CellStyle.ForeColor = Color.Green;
   }
}

But the first line doesn't receive update.

Has anybody else had this problems?

Thanks a lot.

Upvotes: 0

Views: 91

Answers (2)

monteirobrena
monteirobrena

Reputation: 2620

The problem was: the first line was select. To fix just clear the selection of rows after update the ForeColor.

  private void dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
     if (e.RowIndex >= 0) {
        DataGridViewRow row = dgv.Rows[e.RowIndex];
        e.CellStyle.ForeColor = Color.Green;
     }
     dgv.ClearSelection();
  }

Upvotes: 0

user2354869
user2354869

Reputation:

Use CellFormatting event

simply try this:

private void dgv_CellFormatting(object sender, 
                            DataGridViewCellFormattingEventArgs e) 
{
      e.CellStyle.ForeColor = Color.Green;
}

see this picture.

enter image description here

Upvotes: 2

Related Questions