I am using RowDataBound
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 rowdatabound
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
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);
}
}
Why not get the sum from data source?
var gridView = sender as GridView;
var dataSource = gridView.DataSource as IEnumerable<YourDataObject>;
e.Row.Cells[3].Text = dataSource.Sum(item => item.YourProperty).ToString();