gridview中多个页面的grandtotal和runningtotal

时间:2012-11-08 17:02:21

标签: c# gridview running-total

我在页脚上有一个总计的gridview。我的分页每页只允许25条记录。因此,当有多个页面时,应该有总计和总计。我可以帮助我代码。下面是一列的总代码。

<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:";
    }

}

我无法通过样本。那些令人困惑。所以请帮助编码。

更新:

这是我的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)

1 个答案:

答案 0 :(得分:1)

你的代码的运行总数是可以的.. 您可以通过将每个页面的运行总计保存到viewState或Session来计算总计。 或者您想从一开始就显示所有条目的总计?

我假设您不需要所有表的GrandTotal,所以我建议使用另一个varchar变量:@SelectCriteria,您在想要总计的地方设置

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)