2015-03-13 103 views
1

我正在填寫GridView從存儲過程中返回給我。如何計算列數據的總數?

enter image description here

但問題是最後一排,你可以看到它返回我的TotalTransactions總和列,但我想要的是總數的總和只有IE瀏覽器包圍的元素,它生長在

<asp:GridView ID="GridViewConductorTransactions" runat="server" Width="100%" AutoGenerateColumns="False" 
       ShowFooter="True" OnRowDataBound="GridViewConductorTransactions_RowDataBound" CssClass="table table-hover table-striped table-bordered"> 
    <Columns> 
     <asp:TemplateField HeaderText="Conductor Name" HeaderStyle-CssClass="visible-desktop" ItemStyle-CssClass="visible-desktop"> 
      <ItemTemplate> 
       <asp:Label ID="lblConductorName" runat="server" Text='<%# Eval("ConductorName") %>'></asp:Label> 
      </ItemTemplate> 
      <FooterTemplate> 
       <asp:Label ID="totals" runat="server" Text="Totals"></asp:Label> 
      </FooterTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Denominations" HeaderStyle-CssClass="visible-desktop" ItemStyle-CssClass="visible-desktop"> 
      <ItemTemplate> 
       <asp:Label ID="lblDenomination" runat="server" Text='<%# Eval("Denomination") == DBNull.Value ? "Grand Total" : Eval("Denomination") %>'></asp:Label> 
      </ItemTemplate> 
      <%--<FooterTemplate> 
       <asp:Label runat="server" ID="lblTotalDenominations" Font-Bold="true"></asp:Label> 
       </FooterTemplate> --%> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Total Transactions" HeaderStyle-CssClass="visible-desktop" ItemStyle-CssClass="visible-desktop"> 
      <ItemTemplate> 
       <asp:Label ID="lblTotalTransactions" runat="server" Text='<%# Bind("totaltransactions") %>'></asp:Label> 
      </ItemTemplate> 
      <FooterTemplate> 
       <asp:Label runat="server" ID="lblTotalTransactionsSum" Font-Bold="true"></asp:Label> 
      </FooterTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Total Amount" HeaderStyle-CssClass="visible-desktop" ItemStyle-CssClass="visible-desktop"> 
      <ItemTemplate> 
       <asp:Label ID="lblTotalAmount" runat="server" Text='<%# Bind("totalamount") %>'></asp:Label> 
      </ItemTemplate> 
      <FooterTemplate> 
       <asp:Label runat="server" ID="lblTotalAmountSum" Font-Bold="true"></asp:Label> 
      </FooterTemplate> 
     </asp:TemplateField> 

    </Columns> 
</asp:GridView> 

protected void GridViewConductorTransactions_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 

     string lblTotalTransactions = ((Label)e.Row.FindControl("lblTotalTransactions")).Text; 
     TotalTransactionsSum = TotalTransactionsSum + float.Parse(lblTotalTransactions); 

     string lblTotalAmount = ((Label)e.Row.FindControl("lblTotalAmount")).Text; 
     TotalAmountSum = TotalAmountSum + float.Parse(lblTotalAmount); 

    } 

    if (e.Row.RowType == DataControlRowType.Footer) 
    { 

     Label lb5 = (Label)(e.Row.FindControl("lblTotalTransactionsSum")); 
     lb5.Text = TotalTransactionsSum.ToString("n2"); 

     Label lb7 = (Label)(e.Row.FindControl("lblTotalAmountSum")); 
     lb7.Text = TotalAmountSum.ToString("n2"); 
    } 
} 

SP:

SELECT N, 
     case when N = 1 then ConductorName else NULL end ConductorName, 
     Denomination, 
     totaltransactions, 
     totalamount 
    FROM (
    SELECT ROW_NUMBER() OVER (PARTITION BY c.name order by c.name) 'N', 
     CASE WHEN isnull(CAST(T.amount AS varchar(30)), c.name + ' total') LIKE '%total%' 
      THEN NULL 
      ELSE c.name 
     END AS ConductorName, 
     ISNULL(CAST(T.Amount AS varchar(30)), c.Name + ' total') AS Denomination, 
     COUNT(*) AS totaltransactions, 
     SUM(T.Amount) AS totalamount 
    FROM dbo.Tickets AS T INNER JOIN 
      Transport.Conductors AS c ON c.ConductorID = T.Conductor_ID 
    WHERE CONVERT(DATE,ServerDateTime) BETWEEN @FromDate and @ToDate 
    GROUP BY c.Name, T.Amount WITH ROLLUP 
+0

有關循環的GridRow什麼,讓你列的總和? – zey 2015-03-13 06:32:04

+0

不會降低性能,因爲未來數據可能會超過數百萬 – 2015-03-13 06:43:05

+1

您是否要在GridView中顯示數百萬條記錄? – 2015-03-13 13:02:20

回答

1

我會在查詢本身調整的數據。但是你沒有提供關於你的數據的任何細節。所以其他方法是使用GridViewConductorTransactions_RowDataBound方法來獲得你想要的。您可以檢查行包含txttotal則僅總和添加到變量,

if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
      string denominationText = ((Label)e.Row.FindControl("lblDenomination")).Text; 
      if(denominationText.Contains("total")) 
      { 
      string lblTotalAmount = ((Label)e.Row.FindControl("lblTotalAmount")).Text; 
      TotalAmountSum = TotalAmountSum + float.Parse(lblTotalAmount);  

      string lblTotalTransactions = ((Label)e.Row.FindControl("lblTotalTransactions")).Text; 
      TotalTransactionsSum = TotalTransactionsSum + float.Parse(lblTotalTransactions); 
      } 

    } 
+0

先生,SP返回的數據與上面gridview中出現的一樣,請查看 – 2015-03-13 06:42:21

+0

我也有粘貼存儲過程,請檢查! – 2015-03-13 06:42:40

+0

這是我的選擇,回覆任何我想要的,如果他會比我更壞,謝謝你的回答,它幫助@CoderofCode – 2015-03-13 12:42:27