2012-03-03 5 views
0

私は自分の会社の各部門の各クイズの参加者の総数を示すGridViewを持っています。私が今望むのは、これらの数字を合計し、各クイズの後にすべての部門の参加者の合計を表示するための新しい行を追加することです(つまり、1つのクイズにつき小計がある)。 これはどうやって行うのですか?この(要約表)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

総数の確認方法は?私は自分のデータベースにこの番号を持っていません。 –

+0

GetTotal()メソッドで合計を計算する必要があります。 – Bracke

関連する問題