简体   繁体   English

gridview ASP.NET C#中的多个页脚

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



I am trying to show one table to gridview in asp.net c#. 我试图在asp.net c#中向gridview显示一个表。 Using SQL Data Source. 使用SQL数据源。 On the footer, success to show the total of each column. 在页脚上,成功显示每个列的总数。 What I want to do is, I want to add another footer, let say.. the average of each column. 我想做的是,我想添加另一个页脚,说..每列的平均值。

How can i do this? 我怎样才能做到这一点? Advice please. 请咨询。 Thank you. 谢谢。

Below are my current code: 下面是我当前的代码:

ASPX Page: 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>

Code behind: 后面的代码:

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

then I put LoadSummary() on page_Load event. 然后我将LoadSummary()放在page_Load事件上。

You can add any number of rows to the footer using the following method. 您可以使用以下方法将任何数量的行添加到页脚。 Add RowDataBound event handler to the gridview. 将RowDataBound事件处理程序添加到gridview。 Here you need to check whether this is a footer row or not. 在这里,您需要检查这是否是页脚行。 If it is footer row then add another or any number of rows to the naming container of the row. 如果是页脚行,则将另一行或任意数量的行添加到该行的命名容器中。

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

Add a DataBound Method as below 如下添加一个DataBound方法

Code Behind: 背后的代码:

    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