Click here to Skip to main content
14,209,049 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a web page with several databound GridView controls. In the RowDataBound events for these grids, I accumilate the gross total, like this:

Protected Sub TradesGrid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim DVR As DataRowView = CType(e.Row.DataItem, DataRowView)
        If  IsNumeric(DVR("Concession")) Then
            Gross += CDec(DVR("Concession"))
        End If
    End If
End Sub

Once all of the grids have been rendered, I need to put the final gross total at the top of the page, above the grids.

My problem seems to be that the RowDataBound event seems to get fired during the page's render, after my last opportunity to manually change the page's content; even during the PreRenderComplete event, Gross is still zero.

Am I doing something wrong, or will I have to tell management that they can summaries in each grid (which is working fine) but not a full total outside of the grids?
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 2

Maybe if you use the ItemTemplate to get the total (as shown here[^]), it may help you.
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

Dude, you can try helper functions(in codebehind). You can create another function for All gridview total & call that in codebehind after loading all the gridviews. Try this
How to Display Sum Total in the Footer of the GridView Control[^]

EDIT
--------------------------------------
You can do this way
public partial class _Default : System.Web.UI.Page 
{
    decimal grdTotal1 = 0;//GridView1 Total
    decimal grdTotal1 = 0;//GridView2 Total
    protected void Page_Load(object sender, EventArgs e)
    {
       //After GridView bindings
       lblAllGridViewTotal.Text = (grdTotal1 + grdTotal2).ToString();//Displaying All Total value in a label top of the page
    }
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
  decimal rowTotal = Convert.ToDecimal
              (DataBinder.Eval(e.Row.DataItem, "Amount"));
  grdTotal1 = grdTotal1 + rowTotal;//GridView1 Total
 }
 if (e.Row.RowType == DataControlRowType.Footer)
 {
  Label lbl = (Label)e.Row.FindControl("lblTotal");
  lbl.Text = grdTotal1.ToString("c");
 }
}

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
  decimal rowTotal = Convert.ToDecimal
              (DataBinder.Eval(e.Row.DataItem, "Amount"));
  grdTotal2 = grdTotal2 + rowTotal;//GridView2 Total
 }
 if (e.Row.RowType == DataControlRowType.Footer)
 {
  Label lbl = (Label)e.Row.FindControl("lblTotal");
  lbl.Text = grdTotal2.ToString("c");
 }
}
}

Running Total In Gridview Footer in ASP.NET C# VB.NET[^]

BTW you just need to change the code little bit(You can store the every summary values in state like session, viewstate, etc., then you can sum all values & you can get the result.). Let me know please.
   
v3
Comments
Gregory Gadow 25-Jun-11 0:08am
   
Like I said, I can get the summaries for individual grids in each grid's footer. What I need is an aggregate sum of all the grids elsewhere on the page.
thatraja 25-Jun-11 0:45am
   
Check my updated answer please

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



Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190617.2 | Last Updated 25 Jun 2011
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

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