title: How to update DataGridView correctly, so GUI doesn't freeze
body: <p>I have a backgroundworker, which does some calculations and reports progress result as string. This string needs to be inserted into dataGridView. But while inserting values, GUI freezes.</p>
  
  <pre><code>private void bckgrSorter_DoWork(object sender, DoWorkEventArgs e)
  {
      for (int i=0; i&lt;1000; i++)
      {
          // doing some calculations
          bckgrSorter.ReportProgress(i, someString);
      }
  }    
  
   private void bckgrSorter_ProgressChanged(object sender, ProgressChangedEventArgs e)
   {
       string results = (string)e.UserState;
       dataGridView1.Rows.Add(results);
   }
  </code></pre>
  
  <p>So even though I do all the heavy calculations on a background thread, GUI still freezes, because of DataGridView.</p>
  
  <p><strong>Edit for code:</strong></p>
  
  <pre><code>private void bckgrSorter_DoWork(object sender, DoWorkEventArgs e)
      {
          string[] folders = // some folders to get File List from
  
          bckgrFileScanner.RunWorkerAsync(folders);            
      }
  
  private void bckgrFileScanner_DoWork(object sender, DoWorkEventArgs e)
      {
          string[] folders = (string[])e.Argument;
          foreach (string f in folders)
          {
              GetFileList(ref scannedFiles, f, bckgrFileScanner);
              bckgrFileScanner.ReportProgress(1);
          }
      }
  
  public void GetFileList(ref List&lt;FileInfo&gt; fList, string fPath, BackgroundWorker scanner)
          {
              DirectoryInfo di = new DirectoryInfo(fPath);
              FileInfo[] fi = di.GetFiles();
  
              foreach (FileInfo fiTemp in fi)
              {
                  //ar ~$ saakas nevajadzīgie temp faili, tos izlaižam
                  if (fiTemp.Name.StartsWith("~$") == false)
                  {
                      fList.Add(fiTemp);
                      scanner.ReportProgress(0, fiTemp.Name);
                  }
              }
              DirectoryInfo[] dFolders = di.GetDirectories();
  
              //katrai apakšmapei rekursīvi izsaucam šo funkciju
              foreach (DirectoryInfo d in dFolders)
              {
                  GetFileList(ref fList, d.FullName, scanner);
              }
          }
  
  private void bckgrFileScanner_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
          if (e.ProgressPercentage == 0)
          {
              filesDataGrid.Rows.Add(e.UserState);
          }
  
          else progressBar1.PerformStep();
      }
  </code></pre>