我正在尝试在Gridview中进行列式添加但不能。
就像我从商店程序中填充gridview,它提供了这些值。
DateTime 10Quantity 20Quantity 30Quantity 10Amount 20Amount 30Amount GrandTotal
03/10/2015 792 0 594 7920 0 17820 25740
03/10/2015 332 3 194 9990 0 28220 38739
预期:
DateTime 10Quantity 20Quantity 30Quantity 10Amount 20Amount 30Amount GrandTotal
03/10/2015 792 0 594 7920 0 17820 25740
03/10/2015 332 3 194 9990 0 28220 38739
Totals 1124 3 788 17910 0 46040 64479
我想要一个新的行'Totals',它应代表每列的总数,即添加。
代码:
<asp:GridView ID="GridViewDenominationsWiseTransactions" runat="server" Width="100%" AutoGenerateColumns="False"
ShowFooter="True" OnRowDataBound="GridViewDenominationsWiseTransactions_RowDataBound" CssClass="table table-hover table-striped table-bordered">
<Columns>
<asp:BoundField HeaderText="Date" DataField="DateTime" DataFormatString="{0:dd/MMM/yyyy hh:mm}" HtmlEncode="false" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle CssClass="visible-desktop"></HeaderStyle>
<ItemStyle Width="11%" />
</asp:BoundField>
<%-- <asp:BoundField HeaderText="Conductor Name" DataField="ConductorName" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle CssClass="visible-desktop"></HeaderStyle>
<ItemStyle Width="8%"></ItemStyle>
</asp:BoundField>--%>
<asp:BoundField HeaderText="10 Quantity" DataField="10Quantity" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle CssClass="visible-desktop"></HeaderStyle>
<ItemStyle Width="8%" CssClass="visible-desktop"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="20 Quantity" DataField="20Quantity" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle CssClass="visible-desktop"></HeaderStyle>
<ItemStyle Width="8%" CssClass="visible-desktop"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="30 Quantity" DataField="30Quantity" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle CssClass="visible-desktop"></HeaderStyle>
<ItemStyle Width="8%" CssClass="visible-desktop"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="10 Amount" DataField="10Amount" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle CssClass="visible-desktop"></HeaderStyle>
<ItemStyle Width="8%" CssClass="visible-desktop"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="20 Amount" DataField="20Amount" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle CssClass="visible-desktop"></HeaderStyle>
<ItemStyle Width="8%" CssClass="visible-desktop"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="30 Amount" DataField="30Amount" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle CssClass="visible-desktop"></HeaderStyle>
<ItemStyle Width="8%" CssClass="visible-desktop"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="GrandTotal" DataField="GrandTotal" ItemStyle-CssClass="visible-desktop"
HeaderStyle-CssClass="visible-desktop">
<HeaderStyle CssClass="visible-desktop"></HeaderStyle>
<ItemStyle Width="13%" CssClass="visible-desktop"></ItemStyle>
</asp:BoundField>
</Columns>
</asp:GridView>
SP;
ALTER PROCEDURE [dbo].[ReportDenominationWiseTransaction] '03-10-2015', '03-10-2015'
@FromDate date,
@ToDate date
AS
BEGIN
with myquery ([DateTime],amount,Quantity)
as
(
select Convert(varchar(20),serverdatetime,101) 'DateTime',amount,1 'Quantity'
from tickets
WHERE CONVERT(DATE,ServerDateTime) BETWEEN @FromDate and @ToDate
)
select *, 10 * c.[10Quantity] '10Amount',20 * c.[20Quantity] '20Amount',30 * c.[30Quantity] '30Amount',((10 * c.[10Quantity])+(20 * c.[20Quantity])+(30 * c.[30Quantity]))'GrandTotal' from (
SELECT DateTime,[10] AS '10Quantity', [20] AS '20Quantity', [30] AS '30Quantity'
FROM
(SELECT [DateTime], amount,quantity
FROM myquery) p
PIVOT
(
COUNT ([Quantity])
FOR [Amount] IN
( [10], [20], [30])
) AS pvt
) as c
END
我必须从前端进行,即gridview
答案 0 :(得分:0)
使用RowDataBound事件处理程序在将数据添加到GridView时评估数据(总和列)。
添加页脚以显示总数。
看看这个: https://msdn.microsoft.com/en-us/library/bb310552.aspx 第3步:解决方案2。 这就是你使用事件处理程序在GridView中运行总计的方法。
答案 1 :(得分:0)
实际上,您可以使用SUM
在sql
本身找到每列的GROUPING SETS
。
<强>查询强>
SELECT [DateTime],
SUM([10Quantity]) AS [10Quantity],
SUM([20Quantity]) AS [20Quantity],
SUM([30Quantity]) AS [30Quantity],
SUM([10Amount]) AS [10Amount],
SUM([20Amount]) AS [20Amount],
SUM([30Amount]) AS [30Amount],
SUM([GrandTotal]) AS [GrandTotal]
FROM table_name
GROUP BY
GROUPING SETS(([DateTime],[10Quantity],[20Quantity],
[30Quantity],[10Amount],[20Amount],[30Amount],[GrandTotal]),());