title: How to prevent rows in datagrid from flickering while application is running
body: <p>In the application I'm developing at the moment I'm using a datagridview to display data. To fill it, I've to press a button and a backgroundworker will start running, it will fill a datatable and when it's finished running it will use the datatable as the datasource for the datagrid. This works fine, the UI stays responsive et cetera.
  But now I've implemented coloring to the rows, depending on their values (Im still playing around with it, so any suggestions are welcome):</p>
  
  <pre><code>        private void ApplyColoring()
      {
          if (dataGridView1.DataSource != null)
          {
              foreach (DataGridViewRow dataGridRow in dataGridView1.Rows)
              {
                  // hardmap a color to a column
                  IDictionary&lt;Int32, Color&gt; colorDictionary = new Dictionary&lt;Int32, Color&gt;();
                  colorDictionary.Add( 7, Color.FromArgb(194, 235, 211));
                  colorDictionary.Add( 8, Color.Salmon);
                  colorDictionary.Add( 9, Color.LightBlue);
                  colorDictionary.Add(10, Color.LightYellow);
                  colorDictionary.Add(11, Color.LightGreen);
                  colorDictionary.Add(12, Color.LightCoral);
                  colorDictionary.Add(13, Color.Blue);
                  colorDictionary.Add(14, Color.Yellow);
                  colorDictionary.Add(15, Color.Green);
                  colorDictionary.Add(16, Color.White);
  
                  foreach (DataGridViewRow gridRow in dataGridView1.Rows)
                  {
                      foreach (DataGridViewCell cell in gridRow.Cells)
                      {
                          if (colorDictionary.Keys.Contains(cell.ColumnIndex))
                          {
                              // standard background 
                              cell.Style.BackColor = Color.FromArgb(194, 235, 211);
                          }
                      }
                  }
  
                  IList&lt;String&gt; checkedValues = new List&lt;String&gt;();
  
  
                  // first we loop through all the rows
                  foreach (DataGridViewRow gridRow in dataGridView1.Rows)
                  {
                      IDictionary&lt;String, Int32&gt; checkedVal = new Dictionary&lt;String, Int32&gt;();
  
                      // then we loop through all the data columns
                      int maxCol = dnsList.Count + 7;
                      for (int columnLoop = 7; columnLoop &lt; maxCol; columnLoop++)
                      {
                          string current = gridRow.Cells[columnLoop].Value.ToString();
  
                          for (int checkLoop = 7; checkLoop &lt; maxCol; checkLoop++)
                          {
                              string check = gridRow.Cells[checkLoop].Value.ToString();
  
                              if (!current.Equals(check))
                              {
                                  if (checkedVal.Keys.Contains(current))
                                  {
                                      gridRow.Cells[columnLoop].Style.BackColor = colorDictionary[checkedVal[current]];
                                  }
                                  else
                                  {
                                      gridRow.Cells[columnLoop].Style.BackColor = colorDictionary[columnLoop];
                                      checkedVal.Add(current, columnLoop);
                                  }
                              }
                          }
                      }
                  }
              }
          }
      }
  </code></pre>
  
  <p>This is giving me problems. Not because the coloring doesn't work, it does. But because it makes it hella slow. The first time it runs fine, but when i press the button again, it's slow as hell and the datagrid is flickering.
  I want this run as postprocess, so it's (or rather should) be run after the backgroundworker is completed. 
  But when i call applycoloring from the RunWorkerCompleted event its just slow. What should I do to prvent this? How can I make sure the UI doesnt flicker while its executing a new query (while NOT LOSING the current data in the grid).</p>