title: Fastest way to set backcolor for WinForms DataGridView rows
body: <p>I have a WinForms DataGridView that is bound to a BindingSource which, in turn, is bound to a BindingList of 100,000 objects.</p>
  
  <pre><code>BindingList&lt;MyObject&gt; myObjectList = new BindingList&lt;MyObject&gt;();
  BindingSource bindingSourceForMyObjects = new BindingSource();
  
  bindingSourceForMyObjects.DataSource = myObjectList;
  dataGridViewMyObjects.DataSource = bindingSourceForMyObjects;
  </code></pre>
  
  <p>I have an event handler hooked up to my DataGridView's CellValueChanged event that contains the following code:</p>
  
  <pre><code>dataGridViewMyObjects.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
  dataGridViewMyObjects.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.White;
  </code></pre>
  
  <p>So when my user changes a row, this event handler fires and changes the row to white on red to indicate that the data has changed.  This works great but I also have some cases where I need to programatically change the underlying list and I'd like those changes to reflect in the DataGridView, as well.  To accomplish that, my object class implements INotifyPropertyChanged and I have an event handler hooked up to the ListChanged event of my BindingSource.  The code in that event handler looks like this:</p>
  
  <pre><code>if (e.ListChangedType == ListChangedType.ItemChanged)
  {
      dataGridViewMyObjects.Rows[e.NewIndex].DefaultCellStyle.BackColor = Color.Red;
      dataGridViewMyObjects.Rows[e.NewIndex].DefaultCellStyle.ForeColor = Color.White;
  }
  </code></pre>
  
  <p>This is also working so if I programatically modify 50 of the objects, the DataGridView rows get updated as well.  However, as I said before, I have 100,000 objects that I'm dealing with and if I need to modify more than ~800 objects, it can take a while because of all the calls to OnPropertyChanged() in my object.  In the absolute worst case, if I need to modify all 100,000 objects, it can take nearly 1 minute for this to happen.</p>
  
  <p>Inside of my object class, I have a boolean variable which I use to avoid triggering the OnPropertyChanged() call when I'm programatically doing "bulk" updates (> 800 objects).  This makes updating the object properties very fast but the corresponding rows in the DataGridView don't get their forecolor/backcolor values updated anymore since the two-way binding is being bypassed.  I know which rows correspond to the objects that were modified and I've tried looping through them and updating the ForeColor/BackColor values but again, it takes nearly a minute for this operation to complete.</p>
  
  <p>I've tried wrapping that loop in...</p>
  
  <pre><code>dataGridViewMyObjects.SuspendLayout();
  // loop here
  dataGridViewMyObjects.ResumeLayout();
  </code></pre>
  
  <p>but that didn't seem to make a difference in performance.  Is there a faster way to set the ForeColor/BackColor for lots of rows or is the speed I'm seeing simply a matter of the size of the data that I'm working with?</p>