Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hello, i have problem that the total for 3 col that i have will calculate every page alone when, i need to calculate the total for all pages not every page alone using gridview

as u see in code blow i have 3 col, everyone of them will have total but in when i enable paging its give me total for every page and i need total for all pages

thx...

What I have tried:

C#
int RENT = 0;
        int AMOUNTLEFT = 0;
        int AMOUNTPAID = 0;
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
    {
         RENT += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "RENT"));
         AMOUNTLEFT += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, " AMOUNTLEFT"));
         AMOUNTPAID += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, " AMOUNTPAID"));
    }
    // Display totals in the gridview footer
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.Cells[14].Text = "Grand Total المجموع :-";
        e.Row.Cells[14].Font.Bold = true;
 
        e.Row.Cells[16].Text = AMOUNTPAID.ToString();
        e.Row.Cells[16].Font.Bold = true;
       
 
        e.Row.Cells[17].Text = RENT.ToString();
        e.Row.Cells[17].Font.Bold = true;
 
        e.Row.Cells[15].Text = AMOUNTLEFT.ToString();
        e.Row.Cells[15].Font.Bold = true;
 
           
    }
}
Posted
Updated 15-Apr-17 7:40am

1 solution

try like this

if (e.Row.RowType == DataControlRowType.Footer)
            {
                DataTable dt = GridView1.DataSource as DataTable;
                int RENT = 0;
                int AMOUNTLEFT = 0;
                int AMOUNTPAID = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    RENT += Convert.ToInt32(dt.Rows[i]["RentColumnName"]);
                    AMOUNTLEFT += Convert.ToInt32(dt.Rows[i]["AMOUNTLEFTColumnName"]);
                    AMOUNTPAID += Convert.ToInt32(dt.Rows[i]["AMOUNTPAIDColumnName"]);
                }

                e.Row.Cells[14].Text = "Grand Total المجموع :-";
                e.Row.Cells[14].Font.Bold = true;

                e.Row.Cells[16].Text = AMOUNTPAID.ToString();
                e.Row.Cells[16].Font.Bold = true;


                e.Row.Cells[17].Text = RENT.ToString();
                e.Row.Cells[17].Font.Bold = true;

                e.Row.Cells[15].Text = AMOUNTLEFT.ToString();
                e.Row.Cells[15].Font.Bold = true;
             }
 
Share this answer
 
Comments
Learn.net37 15-Apr-17 15:00pm    
getting error Object reference not set to an instance of an object.

here (for (int i = 0; i < dt.Rows.Count; i++))
Karthik_Mahalingam 15-Apr-17 15:15pm    
declare the datatable globally and try accessing the object

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