Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
4.00/5 (1 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:

VB
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

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.
 
Share this answer
 
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
Maybe if you use the ItemTemplate to get the total (as shown here[^]), it may help you.
 
Share this answer
 

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