gridview ASP.NET C#中的多页脚

时间:2014-07-01 08:07:12

标签: c# asp.net gridview



我试图在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事件上。

2 个答案:

答案 0 :(得分:0)

您可以使用以下方法向页脚添加任意数量的行。 将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.
            }
        }

答案 1 :(得分:0)

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