1

I tried using RowPrePaint event of DataGridView to set rows' BackColor conditionally. When I started my application, rows were rendered correctly but there was a problem that the rows were rendered twice.

I am building an application using Windows Form.

private void grd_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
   var row = grd.Rows[e.RowIndex];
   if (row.DataBoundItem is ViewModel model && !model.Materialized)
   {
       row.DefaultCellStyle.BackColor = Color.Orange;
   }
}
  • 1
    I'm not sure why you get the rows twice, I use `DataBindingComplete`, where I fire a tiny function which goes through the rows and handles them in a similar way as your code does. – Oak_3260548 Sep 04 '19 at 12:46
  • 1
    You aren't painting. You are setting a visual property. – LarsTech Sep 04 '19 at 19:07
  • @LarsTech Yes, you are right! I found that because I was setting a visual property so that the row was repainted. – user3598913 Sep 12 '19 at 03:57

1 Answers1

1

I found out that because I only changed row's property in the method. Therefore, the row was repainted. I decided to use Graphics property of the DataGridViewRowPrePaintEventArgs parameter to fill a rectangle as the custom background. After that, I will fill the content to the row except for the background and set e.Handled = true to avoid further painting.