title: Calculate the sum of gridview column data and store its value in a variable
body: <p>I am using <code>RowDataBound</code> Event to calculate the sum of a column data. The variable in which i am getting the sum of column values is becoming zero at the end of the <code>rowdatabound</code> event, because its initial value is zero. How can I store the sum of values in a variable to use its value outside the event. Thanks</p>
  
  <pre><code>int totSubTot = 0;
  public double TotalAmount;
  protected void gvShowOrder_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
          totSubTot += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "SubTotal"));        
      }
      else if (e.Row.RowType == DataControlRowType.Footer)
      {
          e.Row.Cells[2].Text = "Grand Total";
          e.Row.Cells[2].Font.Bold = true;
  
          e.Row.Cells[3].Text = totSubTot.ToString();
          e.Row.Cells[3].Font.Bold = true;
          TotalAmount = Convert.ToDouble(e.Row.Cells[3].Text);
      }
  }
  </code></pre>