如何计算列数据的总数?

时间:2015-03-13 06:19:45

标签: c# sql asp.net tsql gridview

我正在从商店程序中填写GridView,这会将此返回给我。

enter image description here

但问题出在最后一行,您可以看到它返回 TotalTransactions 列的总和,但我想要的只是总数的总和,即环绕的元素,并且它会增长

<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

1 个答案:

答案 0 :(得分:1)

我会调整Query本身的数据。但您尚未提供有关数据的任何详细信息。所以其他方式是使用GridViewConductorTransactions_RowDataBound方法来获得你想要的东西。您可以在total中检查if行是否包含txt,然后只将该总和添加到变量中,

  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);
          }

    }