简体   繁体   中英

Multiple Footer in gridview ASP.NET C#



I am trying to show one table to gridview in asp.net c#. Using SQL Data Source. 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:

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

You can add any number of rows to the footer using the following method. Add RowDataBound event handler to the 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM