title: Calculate sum of a column in a datagridview c#
body: <p>I created a program which calculates the sum of a column in a datagridview, the problem arises when I refactor the logic from the click handler into a separate a method. The <code>calcSum</code> method does not seem to give me the correct output compared with <code>button2_Click</code> which has all the logic directly in the click handler.</p>
  
  <p>Can anyone please point out what's wrong with my code?</p>
  
  <pre><code>//Calls sum of column method, "incorrect" output
  private void button1_Click(object sender, EventArgs e){
      calcSum(a,b,3);
  }
  
  private double calcSum(double a, double b, int c){
      foreach(DataGridViewRow r in dataGridView1.Rows){
          a = Convert.ToDouble(r.Cells[c].Value);
          b = b + a;
      }
      MessageBox.Show("sum is " = b.ToString());
      return b;
  }
  
  //shows correct/calculates output
  private void button2_Click(object sender, EventArgs e){
      double a =0,b=0;
      foreach (DataGridViewRow r in dataGridView1.Rows){
          a = Convert.ToDouble(r.Cells[3].Value);
          b = b + a;
      }
      MessageBox.Show(b.ToString());
  }
  </code></pre>