title: C# - Update GUI datagridview from another thread using delegate
body: <p>I'm trying to update a datagridview with some data calculated in a different class and thread, using a delegate. Unfortunately I'm having trouble with a variety of different errors, depending on the approach I try.</p>
  
  <p>The code I am trying to execute in the form thread looks like this:</p>
  
  <pre><code>public partial class AcquireForm : Form
  //
  // ...
  //
      // update the grid with results
      public delegate void delUpdateResultsGrid(int Index, Dictionary&lt;string, double&gt; scoreCard);
      public void UpdateResultsGrid(int Index, Dictionary&lt;string, double&gt; scoreCard)
      {
          if (!this.InvokeRequired)
          {
              //
              // Some code to sort the data from scoreCard goes here
              //
  
              DataGridViewRow myRow = dataGridViewResults.Rows[Index];
              DataGridViewCell myCell = myRow.Cells[1];
              myCell.Value = 1; // placeholder - the updated value goes here
              }
          }
          else
          {
              this.BeginInvoke(new delUpdateResultsGrid(UpdateResultsGrid), new object[] { Index, scoreCard});
          }
      }
  </code></pre>
  
  <p>Now, I need to get this method to run from my other thread and class. I have tried:</p>
  
  <pre><code>public class myOtherClass
  //
  // ...
  //
  
      private void myOtherClassMethod(int myIndex)
      {
          // ...
          AcquireForm.delUpdateResultsGrid updatedelegate = new AcquireForm.delUpdateResultsGrid(AcquireForm.UpdateResultsGrid);
          updatedelegate(myIndex, myScoreCard);
      }
  </code></pre>
  
  <p>Unfortunately this gives an "Object reference is required for the non-static field, method, or property AcquireForm.UpdateResultsGrid(int, System.Collections.Generic.Dictionary)" error. I seem to be unable to reference the UpdateResultsGrid method at all...</p>
  
  <p>I have noticed that</p>
  
  <pre><code>public class myOtherClass
  //
  // ...
  //
  
      private void myOtherClassMethod(int myIndex)
      {
          // ...
          AcquireForm acquireForm = new AcquireForm();
          acquireForm.UpdateResultsGrid(myIndex,myScoreCard);
      }
  </code></pre>
  
  <p>does not throw any errors when compiling, but it tries to create a new form and that is something I do not want to do. I don't want to create a new instance of AcquireForm, I want to reference the pre-existing one, if that's possible.</p>
  
  <p>I have also tried making the UpdateResultsGrid method static, but this throws up problems with several things incuding the use of "this.(anything)".</p>
  
  <p>I've also tried moving the majority of the UpdateResultsGrid method into myOtherClassMethod, leaving behind in the AcquireForm class just the delegate. Again, this does not work because many of the references to UI objects break (there aren't any dataGridViews in scope).</p>
  
  <p>I'm starting to run out of ideas here. Unfortunately I'm rather new to C# (as you can probably tell), and I'm editing someone else's code rather than writing my own entirely from scratch. If anyone could offer some advice on this problem it'd be most appreciated.</p>