2012-03-03 251 views
0

我有一個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

打開最後一列到模板字段而不是綁定字段:

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

如何找到總數?我的數據庫中沒有這個號碼。 – 2012-03-04 04:57:10

+0

您將必須計算GetTotal()方法中的總數。 – Bracke 2012-03-16 13:02:00