Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How To Find The Total Sum Of Particular Column in Gridview Using C#
Posted
Updated 18-Mar-17 21:28pm

You can iterate through GridViewRow and find particular control in which number is displayed and do summation of it as below.

C#
var total = 0;
            foreach (GridViewRow row in GridView1.Rows)
            {
                var numberLabel = row.FindControl("ID of Label") as Label;
                int number;
                if (int.TryParse(numberLabel.Text, out number))
                {
                    total += number;
                }
            }

            // Sum in variable total
 
Share this answer
 
Comments
[no name] 6-May-13 5:57am    
label id means i have not taken any labels inside the gridview.,
vijay__p 6-May-13 6:01am    
Have you used bound column?
vijay__p 6-May-13 6:02am    
If you have used bound column then you can use below code
var numberLabel = row.Cells[1].Text; // 1 is the column number in GridView
[no name] 6-May-13 6:03am    
i have not used bound column
vijay__p 6-May-13 6:11am    
Then what you have used ?
Try this

C#
int sum = 0;
       for (int i = 0; i < GridView1.Rows.Count; i++)
       {
           sum += int.Parse( GridView1.Rows[i].Cells[CellIndex].Text);
       }
       Label1.Text = sum.ToString();


Enter Cell Index as per your requirement, which column you want.
 
Share this answer
 
Comments
[no name] 6-May-13 6:05am    
it given the error like:Input string was not in a correct format.
[no name] 6-May-13 6:08am    
i want the sum of the 5th column in gridview

int sum = 0;
for (int i = 0; i < grv_Product.Rows.Count; i++)
{
sum += int.Parse(grv_Product.Rows[i].Cells[5].Text);
}
Bikash Prakash Dash 6-May-13 6:10am    
for 5th column cell index is 4.
Member 12498070 4-May-16 7:26am    
it was a much helpful solution. Thnx a lot!
Here is the example. You can implement this in RowDataBound Event of Gridview as follows:


C#
protected void Gridview_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string name= textbox1.text;
                SqlCommand cmd = new SqlCommand("select SUM(leave) from leave_rec where name=@empname",conn);
                cmd.Parameters.Add(new SqlParameter("@empname", name));
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    Label leave_label = (Label)e.Row.FindControl("your label id");
                    object value = dr.GetValue(0);
                    leave_label.Text = value.ToString();
                }
             }
         }


Here,we have label field(leave_label) as templatefiled in gridview.On Gridview_RowDataBound Event,we have calculated total leaves for particuler employee which will be displayed in label.
 
Share this answer
 
v2
Sum of Columns of 27 GridViews at once:
protected void Page_Load(object sender, EventArgs e)
{
//== Array of Controls =============================
GridView[] ObjGridViewArray = new GridView[26];
ObjGridViewArray[0] = Gridview4;
ObjGridViewArray[1] = Gridview5;
ObjGridViewArray[2] = Gridview6; ObjGridViewArray[3] = Gridview7;
ObjGridViewArray[4] = Gridview9; ObjGridViewArray[5] = Gridview10;
ObjGridViewArray[6] = Gridview11; ObjGridViewArray[7] = Gridview12; ObjGridViewArray[8] = Gridview13; ObjGridViewArray[9] = Gridview14;
ObjGridViewArray[9] = Gridview16; ObjGridViewArray[10] = Gridview17; ObjGridViewArray[11] = Gridview18; ObjGridViewArray[12] = Gridview19; ObjGridViewArray[13] = Gridview20;
ObjGridViewArray[14] = Gridview21; ObjGridViewArray[15] = Gridview22; ObjGridViewArray[16] = Gridview23; ObjGridViewArray[17] = Gridview24; ObjGridViewArray[18] = Gridview25;
ObjGridViewArray[19] = Gridview26; ObjGridViewArray[20] = Gridview27; ObjGridViewArray[21] = Gridview28; ObjGridViewArray[22] = Gridview29; ObjGridViewArray[23] = Gridview30;
ObjGridViewArray[24] = Gridview31; ObjGridViewArray[25] = Gridview33;
for (int i = 0; i < ObjGridViewArray.Length; i++)
{

sumOfColumnsOfAllGridviews(ObjGridViewArray[i]);
}
}
private void sumOfColumnsOfAllGridviews(GridView ObjGridViewArrays)
{
string valuesG;

for (int c = 0; c < ObjGridViewArrays.Columns.Count; c++)
{
for (int j = 0; j < ObjGridViewArrays.Rows.Count; j++)
{
valuesG = ObjGridViewArrays.Rows[j].Cells[c].Text;
double Num;
bool isNum = double.TryParse(valuesG, out Num);
if (isNum)
{
sum2 = sum2 + decimal.Parse(valuesG);
}
//== Display Total =======================
ObjGridViewArrays.FooterRow.Cells[0].Text = "Total";
ObjGridViewArrays.FooterRow.Cells[c].Text = sum2.ToString();
ObjGridViewArrays.FooterRow.Cells[c].BackColor = System.Drawing.Color.Khaki;
ObjGridViewArrays.FooterRow.Cells[0].BackColor = System.Drawing.Color.Red;
//========================================
}
sum2 = 0;
}
}
 
Share this answer
 
v3
Comments
CHill60 19-Mar-17 12:45pm    
The question is nearly 4 years old. I have no idea why you have decided to extend the problem stated to multiple gridviews and I'm not convinced that this unformatted code does what you think it does.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900