如何在这个(摘要表)GridView中的每个测验后显示总数?

时间:2012-03-03 09:12:31

标签: c# asp.net gridview

我有一个GridView,显示我公司每个部门的每个测验的参与者总数。我现在想要的是将这些数字相加并添加一个新行,以显示每个测验后所有部门的参与者总数(即每个测验都有一个小数据)。 那怎么做?

我的ASP.NET代码:

<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>

我的守则 - 背后:

protected void GridView3_DataBinding(object sender, EventArgs e)
        {
            GridViewGroup first = new GridViewGroup(GridView3, null, "Title");
        }

        protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //for adding a spearator between rows
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if ((e.Row.RowIndex % 7) == 0)
                {
                    foreach (TableCell c in e.Row.Cells)
                        c.Attributes.Add("Style", "border-top: #BBD9EE 5px double ");
                }
            }
        }

当前表的快照: enter image description here

1 个答案:

答案 0 :(得分:1)

将最后一列转换为模板字段而不是绑定字段:

<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方法添加到后面的代码中,计算总和。

您可能希望将ShowFooter="true"添加到gridview声明中。

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