Monday 11 June 2012

how to calculate sum of gridview columns in .net

Here, we are going to calculate the total value of a perticular column in GridView1.



As you can see in above fig.. there is QUANTITY, RATE, AMOUNT columns are there..
Suppose we have a requirement where we want to calculate the total number QUANTITY for BE No.- BOE-1.
For that reason, i have written bellow code in ASP.NET(C#)

decimal a=0, b=0;   // declaring variable
for (int i = 0; i < (gridview1.Rows.Count); i++)     //Counting total number of rows in GridView1
{

    a = Convert.ToDecimal(gridview1.Rows[i].Cells[7].Text.ToString());
    b = b + a; //storing total qty into variable

}

Note-
1)  Convert.ToDecimal(gridview1.Rows[i].Cells[7].Text.ToString()); //here i have given "Rows[i].Cells[7].Text" Becouse, i want to sum quantity and its index is  7.

http://www.codeproject.com/Questions/271994/how-to-calculate-sum-of-gridview-columns-in-net