C# 如何在此(汇总表)网格视图中显示每次测验后的总数?
我有一个GridView,它显示了我公司每个部门每个测验的参与者总数。我现在想要的是将这些数字相加,并添加一个新行,以显示每个测验后所有分区的参与者总数(即,每个测验有一个子总数)那么怎么做呢? 我的ASP.NET代码:C# 如何在此(汇总表)网格视图中显示每次测验后的总数?,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个GridView,它显示了我公司每个部门每个测验的参与者总数。我现在想要的是将这些数字相加,并添加一个新行,以显示每个测验后所有分区的参与者总数(即,每个测验有一个子总数)那么怎么做呢? 我的ASP.NET代码: <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDa
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource6" CssClass="datatable"
CellPadding="0" BorderWidth="0px" GridLines="None"
OnDataBinding="GridView3_DataBinding" OnRowDataBound="GridView3_RowDataBound" AllowPaging="True" PageSize="16">
<Columns>
<asp:BoundField DataField="Title" HeaderText="Quiz"
SortExpression="Title" />
<asp:BoundField DataField="DivisionShortcut"
HeaderText="Division"
SortExpression="DivisionShortcut" />
<asp:BoundField DataField="Total Number of Participants"
HeaderText="Total Number of Participants" ReadOnly="True"
SortExpression="Total Number of Participants"/>
</Columns>
<RowStyle CssClass="row" />
<SortedAscendingCellStyle CssClass="sortasc"></SortedAscendingCellStyle>
<SortedAscendingHeaderStyle CssClass="sortasc"></SortedAscendingHeaderStyle>
<SortedDescendingHeaderStyle CssClass="sortdesc"></SortedDescendingHeaderStyle>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource6" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT dbo.Quiz.Title, dbo.Divisions.DivisionShortcut, COUNT(DISTINCT dbo.UserQuiz.Username) AS [Total Number of Participants]
FROM dbo.employee INNER JOIN
dbo.UserQuiz ON dbo.employee.Username = dbo.UserQuiz.Username INNER JOIN
dbo.Quiz ON dbo.UserQuiz.QuizID = dbo.Quiz.QuizID INNER JOIN
dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode
GROUP BY dbo.Quiz.Title, dbo.Divisions.DivisionShortcut
ORDER BY dbo.Quiz.Title"></asp:SqlDataSource>
当前表的快照:
将最后一列转换为模板字段而不是绑定字段:
<asp:TemplateField HeaderText="Total Number of Participants">
<ItemTemplate >
<span><%#DataBinder.Eval(Container.DataItem, "Total Number of Participants")%></span>
</ItemTemplate>
<FooterTemplate><%# GetTotal() %></FooterTemplate>
</asp:TemplateField>
如何找到总数?我的数据库中没有这个数字。您必须在GetTotal()方法中计算总数。
<asp:TemplateField HeaderText="Total Number of Participants">
<ItemTemplate >
<span><%#DataBinder.Eval(Container.DataItem, "Total Number of Participants")%></span>
</ItemTemplate>
<FooterTemplate><%# GetTotal() %></FooterTemplate>
</asp:TemplateField>
protected int GetTotal()
{
int total = 0;
using (var Connection = new SqlCeConnection("<testConnectionString>"))
using (var Command = new SqlCeCommand("<SQL statement>"))
using (var Reader = Command.ExecuteReader())
{
while (Reader.Read())
{
total += (int)Reader["Total Number of Participants"];
}
}
return total;
}