簡體   English   中英

gridview ASP.NET C#中的多個頁腳

[英]Multiple Footer in gridview ASP.NET C#



我試圖在asp.net c#中向gridview顯示一個表。 使用SQL數據源。 在頁腳上,成功顯示每個列的總數。 我想做的是,我想添加另一個頁腳,說..每列的平均值。

我怎樣才能做到這一點? 請咨詢。 謝謝。

下面是我當前的代碼:

ASPX頁面:

<asp:GridView runat="server" ID="gvGrid" AutoGenerateColumns="False" GridLines="None" PageSize="27" ShowFooter="true" DataKeyNames="ID" DataSourceID="myDataSource">
<Columns>
<asp:BoundField DataField="Time" HeaderText="Time" SortExpression="OperationTime" HeaderStyle-ForeColor="Green" />     
<asp:BoundField DataField="Number1" HeaderText="Number1" SortExpression="Number1" />
<asp:BoundField DataField="Number2" HeaderText="Number2" SortExpression="Number2" />
<asp:BoundField DataField="Number3" HeaderText="Number3" SortExpression="Number3" />
<asp:BoundField DataField="Number4" HeaderText="Number4" SortExpression="Number4" />
<asp:BoundField DataField="Number5" HeaderText="Number5" SortExpression="Number5" />
</Columns>

<FooterStyle Font-Bold="true" />
</asp:GridView>

<asp:SqlDataSource ID="myDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:myConn %>" SelectCommand="_spShowList" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="txtDate" Name="Date" PropertyName="Text" Type="DateTime" />
</SelectParameters>
</asp:SqlDataSource>

后面的代碼:

protected void LoadSummary()
            {
                SqlConnection conConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["myConn"].ConnectionString);

                SqlCommand cmdLoadUnit = new SqlCommand();
                cmdLoadUnit.CommandType = CommandType.StoredProcedure;
                cmdLoadUnit.CommandText = "_spSUMReport";

                cmdLoadUnit.Parameters.AddWithValue("@Date", txtDate.Text);
                cmdLoadUnit.Connection = conConnection;

                try
                {
                    conConnection.Open();

                    using (SqlDataReader myReader = cmdLoadUnit.ExecuteReader())
                    {
                        while (myReader.Read())
                        {
                            gvGrid.Columns[0].FooterText = "Total";
                            gvGrid.Columns[1].FooterText = String.Format(CultureInfo.InvariantCulture, "{0:0,0.0}", myReader["Number1"]);
                            gvGrid.Columns[2].FooterText = String.Format(CultureInfo.InvariantCulture, "{0:0,0.0}", myReader["Number2"]);
                            gvGrid.Columns[3].FooterText = String.Format(CultureInfo.InvariantCulture, "{0:0,0.0}", myReader["Number3"]);
                            gvGrid.Columns[4].FooterText = String.Format(CultureInfo.InvariantCulture, "{0:0,0.0}", myReader["Number4"]);
                            gvGrid.Columns[5].FooterText = String.Format(CultureInfo.InvariantCulture, "{0:0,0.0}", myReader["Number5"]);
                        }
                    }
                }
                finally
                {
                    conConnection.Close();
                }
            }

然后我將LoadSummary()放在page_Load事件上。

您可以使用以下方法將任何數量的行添加到頁腳。 將RowDataBound事件處理程序添加到gridview。 在這里,您需要檢查這是否是頁腳行。 如果是頁腳行,則將另一行或任意數量的行添加到該行的命名容器中。

        protected void AdminSearchGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {

                TableRow tableRow = new TableRow();
                TableCell cell1 = new TableCell();
                cell1.Text = "Add your summary here"; // Get the calculation from database and display here
                cell1.ColumnSpan = 6; // You can change this
                tableRow.Controls.AddAt(tableRow.Controls.Count,cell1);
                e.Row.NamingContainer.Controls.Add(tableRow);
                // You can add additional rows like this.
            }
        }

如下添加一個DataBound方法

背后的代碼:

    Protected Sub gvGrid_DataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvGrid.DataBound

    Dim grid as GridView = CType(sender, GridView)

    ''To Clone Current Footer
    Dim footer As GridViewRow = grid.FooterRow
    Dim numCells = footer.Cells.Count

    Dim newRow As New GridViewRow(footer.RowIndex + 1, -1, footer.RowType, footer.RowState)

    ''To add correct number of cells
    ''To Copy styles over from the original footer
    For i As Integer = 0 To numCells - 1
        Dim emptyCell As New TableCell
        emptyCell.ApplyStyle(grid.Columns(i).ItemStyle)

        newRow.Cells.Add(emptyCell)
    Next

    newRow.Cells(0).Text = "Avg of 1st column" 'Calculate the average and assign it
    newRow.Cells(1).Text = "Avg of 2nd column"
    newRow.Cells(2).Text = "Avg of 3rd column"
    newRow.Cells(3).Text = "Avg of 4th column"
    newRow.Cells(4).Text = "Avg of 5th column"
    newRow.Cells(5).Text = "Avg of 6th column"

    ''Add the generated New RoW at end of gridView
    CType(grid.Controls(0), Table).Rows.Add(newRow)

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM