Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server
i have 6 columns (salary,ta,contingency,nrc,institcharges,others) in my gridview what i wanna do is i want to get the sum of each column in the footer of each column. for this i have taken template fields and inserted a label in them but my problem is in my coding the result i am getting is total of all the columns on each label
 
this is the code i m using in my code behind file:
 
int total = 0;
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
 

 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "salary"));
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label sallabel = (Label)e.Row.FindControl("sallabel");
            sallabel.Text = total.ToString();
        }
 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ta"));
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblamount2 = (Label)e.Row.FindControl("talabel");
            lblamount2.Text = total.ToString();
 
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "contigency"));
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblamount3 = (Label)e.Row.FindControl("cnlabel");
            lblamount3.Text = total.ToString();
        }
 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "nrc"));
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblamount4 = (Label)e.Row.FindControl("nrclabel");
            lblamount4.Text = total.ToString();
 
        }
 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "institcharges"));
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblamount5 = (Label)e.Row.FindControl("iclabel");
            lblamount5.Text = total.ToString();
        }
 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "others"));
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblamount6 = (Label)e.Row.FindControl("olabel");
            lblamount6.Text = total.ToString();
        }
 
    }
Posted 6-Dec-12 9:04am
Edited 6-Dec-12 9:08am
ridoy51.8K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
a2ulthakur at 6-Dec-12 20:55pm
   
in all examples there is just one column which is being added in my case there are 6 columns and i want to add all of them separately and get their individual total not the cumulative total, i want cumulative total also but that should be after each column's total is displayed in the end i wanna show grand total and add all of the columns
ridoy at 7-Dec-12 1:07am
   
I see your code..the problem may be for total variable...look you use 1 total variable for every column total for which it prints same results.use different variable for every case(salary,ta..etc).Otherwise your code is ok.
a2ulthakur at 7-Dec-12 2:54am
   
thanks for the help..and ya i just needed to add some variables for every column that's it:
int t1=0;
int t2=0;
int t3=0;
int t4=0;
int t5=0;
int t6=0;
thats it !!
ridoy at 7-Dec-12 3:21am
   
glad to see you find solution..happy coding..:)

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 883
1 OriginalGriff 410
2 CPallini 275
3 George Jonsson 166
4 ChauhanAjay 150
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,482
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100