title: C# Update DataGridView from BackgroundWorker
body: <p>Still struggling to get live data from my database to datagridview. This time I am attempting to do it via background worker. The only thing it gives from doWork method is an INT for backgroundWorker1_ProgressChanged method. I want to send my objects such as DataTable to it as well, so I could refresh data which is on main thread Form1. How can I pass my objects to backgroundWorker1_ProgressChanged event? Marked the place in the code where I need the data from DoWork</p>
  
  <pre><code>    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
      {
          string IP_db_mysql = "1";
          string Port_db__mysql = "1";
          string UserName_db__mysql = "1";
          string Password_db__mysql = "1";
          string dbName__mysql = "1";
  
          string connString = "Server =" + IP_db_mysql + "; Port =" + Port_db__mysql + "; Database =" + dbName__mysql + "; Uid =" + UserName_db__mysql + ";Pwd =" + Password_db__mysql + ";";
          MySqlConnection conn = new MySqlConnection();
          DataTable dt = new DataTable();
  
          string query = "SELECT * FROM mysql.test_table;";
  
          int sum = 0;
  
          for (int i = 1; i &lt;= 10; i++)
          {
              sum = sum + i;
  
              try
              {
                  // open connectipon
                  conn.ConnectionString = connString;
                  conn.Open();
  
                  // build command
                  MySqlCommand cmd = new MySqlCommand(query, conn);
  
                  // execute command
                  MySqlDataReader dataReader = cmd.ExecuteReader();
  
                  // fill datatable
                  dt.Load(dataReader);
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
              finally
              {
                  conn.Close();
              }
  
              // Calling ReportProgress() method raises ProgressChanged event
              // To this method pass the percentage of processing that is complete
              backgroundWorker1.ReportProgress(i);
  
              Thread.Sleep(100);
  
              // Check if the cancellation is requested
              if (backgroundWorker1.CancellationPending)
              {
                  // Set Cancel property of DoWorkEventArgs object to true
                  e.Cancel = true;
                  // Reset progress percentage to ZERO and return
                  backgroundWorker1.ReportProgress(0);
                  return;
              }
          }
  
          // Store the result in Result property of DoWorkEventArgs object
          e.Result = sum;
      }
  
      private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
          progressBar1.Value = e.ProgressPercentage;
          labelProgress.Text = e.ProgressPercentage.ToString() + "%";
       **-----------&gt;&gt;   dataGridView1.DataSource = HOW TO GET DATATABLE FROM DO WORK METHOD !?  &lt;&lt;------------------------**
      }
  
      private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          if (e.Cancelled)
          {
              labelProgress.Text = "Processing cancelled";
          }
          else if (e.Error != null)
          {
              labelProgress.Text = e.Error.Message;
          }
          else
          {
              labelProgress.Text = e.Result.ToString();
          }
      }
  
      private void button23_Click(object sender, EventArgs e)
      {
          // Check if the backgroundWorker is already busy running the asynchronous operation
          if (!backgroundWorker1.IsBusy)
          {
              // This method will start the execution asynchronously in the background
              backgroundWorker1.RunWorkerAsync();
          }
      }
  
      private void button24_Click(object sender, EventArgs e)
      {
          if (backgroundWorker1.IsBusy)
          {
              // Cancel the asynchronous operation if still in progress
              backgroundWorker1.CancelAsync();
          }
      }
  </code></pre>