【问题标题】:Multiple Footer in gridview ASP.NET C#gridview ASP.NET C#中的多个页脚
【发布时间】:2014-07-02 12:28:23
【问题描述】:



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

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    如下添加数据绑定方法

    代码背后:

        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
    

    【讨论】:

    • 你能用 C# 代码分享它吗?真的很感激。谢谢。
    【解决方案2】:

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

    【讨论】:

    • 嗨,实现你的代码..但是新行没有跟随上一行..格式不同。干杯,
    • 我对代码做了一些改动。要跟随行到上一行,您需要使用 AddAt 函数在末尾添加,而不是 Add。关于格式,我已将列跨度更改为 6。如果仍需要 6 个列,则需要添加 6 个单元格。我刚刚在单元格上添加并标记为跨越 6 列
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    相关资源
    最近更新 更多