title: Update DataGridView cells from different thread in C# winforms
body: <p>How can I update each DataGridView cell from a different thread? Basically, I have 3 threads (main thread "winform", thread 1 and thread 2), moreover, thread 1 and thread 2 are in different class (class 2) and main thread in class 1. I made this work in one class, but I couldn't do in different class. Please any help would be appreciated. Please see the code below which works in one class:</p>
  
  <pre><code>// THIS IS THE DESIGNER FORM CLASS
  namespace DelegatesAndCallback
  {
  partial class class1
  {
      /// &lt;summary&gt;
      /// Variable nécessaire au concepteur.
      /// &lt;/summary&gt;
      private System.ComponentModel.IContainer components = null;
  
      /// &lt;summary&gt;
      /// Nettoyage des ressources utilisées.
      /// &lt;/summary&gt;
      /// &lt;param name="disposing"&gt;true si les ressources managées doivent être supprimées ; sinon, false.&lt;/param&gt;
      protected override void Dispose(bool disposing)
      {
          if (disposing &amp;&amp; (components != null))
          {
              components.Dispose();
          }
          base.Dispose(disposing);
      }
  
      #region Code généré par le Concepteur Windows Form
  
      /// &lt;summary&gt;
      /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
      /// le contenu de cette méthode avec l'éditeur de code.
      /// &lt;/summary&gt;
      private void InitializeComponent()
      {
          this.button1 = new System.Windows.Forms.Button();
          this.pb = new System.Windows.Forms.ProgressBar();
          this.dgv = new System.Windows.Forms.DataGridView();
          this.Number = new System.Windows.Forms.DataGridViewTextBoxColumn();
          ((System.ComponentModel.ISupportInitialize)(this.dgv)).BeginInit();
          this.SuspendLayout();
          // 
          // button1
          // 
          this.button1.Location = new System.Drawing.Point(203, 34);
          this.button1.Name = "button1";
          this.button1.Size = new System.Drawing.Size(75, 23);
          this.button1.TabIndex = 0;
          this.button1.Text = "button1";
          this.button1.UseVisualStyleBackColor = true;
          this.button1.Click += new System.EventHandler(this.button1_Click);
          // 
          // pb
          // 
          this.pb.Location = new System.Drawing.Point(16, 415);
          this.pb.Name = "pb";
          this.pb.Size = new System.Drawing.Size(443, 23);
          this.pb.TabIndex = 2;
          // 
          // dgv
          // 
          this.dgv.AllowUserToAddRows = false;
          this.dgv.AllowUserToDeleteRows = false;
          this.dgv.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
          this.dgv.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
          this.Number});
          this.dgv.Location = new System.Drawing.Point(167, 76);
          this.dgv.Name = "dgv";
          this.dgv.Size = new System.Drawing.Size(146, 320);
          this.dgv.TabIndex = 3;
          // 
          // Number
          // 
          this.Number.HeaderText = "Number";
          this.Number.Name = "Number";
          // 
          // Form1
          // 
          this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
          this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
          this.ClientSize = new System.Drawing.Size(479, 450);
          this.Controls.Add(this.dgv);
          this.Controls.Add(this.pb);
          this.Controls.Add(this.button1);
          this.Name = "Form1";
          this.Text = "Form1";
          ((System.ComponentModel.ISupportInitialize)(this.dgv)).EndInit();
          this.ResumeLayout(false);
  
      }
  
      #endregion
  
      private System.Windows.Forms.Button button1;
      private System.Windows.Forms.ProgressBar pb;
      private System.Windows.Forms.DataGridView dgv;
      private System.Windows.Forms.DataGridViewTextBoxColumn Number;
  }
  }
  </code></pre>
  
  <p>this is class1 main thread</p>
  
  <pre><code>// THIS IS CLASS1 MAIN THREAD
  using System;
  using System.Collections;
  using System.Threading;
  using System.Windows.Forms;
  
  namespace DelegatesAndCallback
  {
  public partial class class1 : Form
  {
      private Thread newThread1;
      private Thread newThread2;
      private int i = 0;
  
      public Form1()
      {
          InitializeComponent();
  
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          dgv.Paint += dgv_Paint;
  
          // thread #1
          newThread1 = new Thread(Process1);
          newThread1.Name = "Thread 1";
          newThread1.Start();
          newThread1.Join();
  
          // thread #2
          newThread2 = new Thread(Process2);
          newThread2.Name = "Thread 2";
          newThread2.Start();
          newThread2.Join();
      }
  
      private void dgv_Paint(object sender, PaintEventArgs e)
      {
          //Refresh();
      }
  
      public void Process1()
      {
          for (; i &lt; 10; i++)
          {
              int i1 = i;
              dgv.BeginInvoke(new MethodInvoker(() =&gt; dgv.Rows.Add(i1,i1)));
              pb.BeginInvoke(new MethodInvoker(() =&gt; updateValue(i1)));
              dgv.BeginInvoke(new MethodInvoker(() =&gt; dgv.Update()));
              dgv.BeginInvoke(new MethodInvoker(() =&gt; pb.Refresh()));
              Thread.Sleep(200);
          }
      }
  
      public void updateValue(double i1)
      {
          pb.Value = (int)(100*(i1/19));
      }
  
      public void Process2()
      {
          for (; i &lt; 20; i++)
          {
              int i1 = i;
              dgv.BeginInvoke(new MethodInvoker(() =&gt; dgv.Rows.Add(i1, i1)));
              pb.BeginInvoke(new MethodInvoker(() =&gt; updateValue(i1)));
              dgv.BeginInvoke(new MethodInvoker(() =&gt; dgv.Update()));
              dgv.BeginInvoke(new MethodInvoker(() =&gt; pb.Refresh()));
              Thread.Sleep(200);
          }
      }
  }
  }
  </code></pre>
  
  <p>this is class2 where i would like to update the datagridview</p>
  
  <pre><code>using System;
  using System.Collections;
  using System.Threading;
  using System.Windows.Forms;
  
  namespace DelegatesAndCallback
  {
  class DelegateExample
  {
      // Declaration
      public void Process1(){
        // HERE NEED TO HELP TO UPADTE/REFRESH AND SET DATEGRIDVIEW VALUE FROM CLASS1  
      }
  
      public void Process2(){
        // HERE NEED TO HELP TO UPADTE/REFRESH AND SET DATEGRIDVIEW VALUE FROM CLASS1  
      }
  }
  }
  </code></pre>