Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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
Updated 6-Dec-12 9:08am
v2

1 solution

 
Share this answer
 
Comments
a2ulthakur 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 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 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 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)



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