Sam
Sam

Reputation: 301

grandtotal and runningtotal for multiple pages in gridview

I have a gridview with total at the footer. My paging allows only 25 records for each page. So when there are multiple pages there should be running total and the grand total. Could me anyone help me with the code. below are the code for the total for a column.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="true" ShowFooter="True" OnPageIndexChanging="GridView1_PageIndexChanging" OnDataBound="GridView1_DataBound" OnRowDataBound="GridView1_RowDataBound" OnRowCreated="GridView1_RowCreated">
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="30px"                                                                                                          HeaderStyle-VerticalAlign="Top" ItemStyle-Wrap="True">
<ItemTemplate>
<asp:Label ID="lblcriteria" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CRITERIA_N") %>' />
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Period" SortExpression="PERIOD_N" ItemStyle-HorizontalAlign="Center"
 HeaderStyle-VerticalAlign="Top" ItemStyle-Wrap="True">
<ItemTemplate>
<asp:Label ID="lblPERIOD_N" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"PERIOD_N") %>' /></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Receipts" SortExpression="CHECK_AMT_A_USD" ItemStyle-HorizontalAlign="Center"
HeaderStyle-VerticalAlign="Top" HeaderStyle-Width="70px">
<ItemTemplate>
<asp:Label ID="lblReceipt" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CHECK_AMT_A_USD") %>' /></ItemTemplate>
<FooterTemplate>
<asp:Label ID="lbltotReceipt" runat="server"></asp:Label>
<asp:Label ID="lblgrangtot" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</column>

aspx.cs

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridView grid = sender as GridView;
    DataRowView tableData = e.Row.DataItem as DataRowView;
    DataTable dt = Session["List"] as DataTable;       

    if (e.Row.RowType == DataControlRowType.Header)
    {
        totreceipt = 0; 
    }
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label lblReceipt = (Label)e.Row.FindControl("lblReceipt");
        totreceipt += Convert.ToDecimal(tableData["CHECK_AMT_A_USD"]);
    }
    if (e.Row.RowType == DataControlRowType.Footer)
    {

        Label lbltotReceipt = (Label)e.Row.FindControl("lbltotReceipt");
        lbltotReceipt.Text = string.Format("{0:N2}", totreceipt);
        e.Row.Cells[0].Text = "Total:";
    }

}

I couldn't go through the sample ones. Those are pretty confusing. So Kindly help with the coding.

UPDATE:

Here is my SP..

CREATE procedure [dbo].[SP_AVG_DAYS]      

@Cont_C char(1),      
@Month_dd varchar(10),        
@MonYr_S VARCHAR(12),        
@2_Div_Code VARCHAR(2)       

As                  

DECLARE @SelectText varChar(2000)                         
DECLARE @Table varchar(30)      

IF @2_Div_Code <> ''    
BEGIN    
 IF @MonYr_S='Div_Reg'    
 BEGIN    
  SET @Table='VW_BPDP_DIV_REG'    
 END    
 IF @MonYr_S='Div_Area'    
 BEGIN     
  SET @Table='VW_BPDP_DIV_AREA'    
 END    
 IF @MonYr_S='Div_Loc'    
 BEGIN     
  SET @Table='VW_BPDP_DIV_LOC'    
 END 
 END


SET @SelectText='SELECT CRITERIA_N,HEADER_N,PERIOD_N,CHECK_RECEIPT, BP_DAYS 
FROM ' + @Table + '       
WHERE  CONT_C = ''' + @Cont_C + ''' AND DIV_C = ''' + @2_Div_Code + ''' '     

EXECUTE(@SelectText)

Upvotes: 0

Views: 1059

Answers (1)

JJschk
JJschk

Reputation: 431

the running total is ok from your code.. the grand total you can compute it by saving the running total from each page into viewState or Session. Or do you want to show the grand total for all entries from the beginning?

I assume you do not need the GrandTotal for all tables, so I would suggest to have another varchar variable : @SelectCriteria which you set in the places you would like to have a grand total

CREATE procedure [dbo].[SP_AVG_DAYS]   
@Cont_C char(1),      
@Month_dd varchar(10),        
@MonYr_S VARCHAR(12),        
@2_Div_Code VARCHAR(2),        
@2_Reg_Code VARCHAR(3),        
@2_Area_Code VARCHAR(3),        
@2_CR_Code VARCHAR(3)       

As                  

DECLARE @SelectText varChar(2000)                         
DECLARE @SelectCriteria varChar(100)    
DECLARE @Table varchar(30)      

IF @2_Div_Code <> ''    
BEGIN    
IF @MonYr_S='Div_Reg'    
 BEGIN    
   SET @SelectCriteria =', Sum(_columnWithValues) as GrandTotal'
   SET @Table='IA_VPAYMENTS_BPDP_DIV_REG'    
 END    
IF @MonYr_S='Div_Area'    
  BEGIN     
SET @Table='IA_VPAYMENTS_BPDP_DIV_AREA'    

END    
 IF @MonYr_S='Div_Loc'    
 BEGIN     
  SET @Table='IA_VPAYMENTS_BPDP_DIV_LOC'    
 END 
 END


SET @SelectText='SELECT CRITERIA_N,HEADER_N,PERIOD_N,CHECK_RECEIPT, BP_DAYS' + @SelectCriteria + ' FROM ' + @Table + 'WHERE  CONT_C = ''' + @Cont_C + ''' AND DIV_C = ''' + @2_Div_Code + ''' '     
EXECUTE(@SelectText)

Upvotes: 1

Related Questions