【问题标题】:ASP.NET Adding Multiple footers rows to GridviewASP.NET 向 Gridview 添加多个页脚行
【发布时间】:2011-08-17 02:49:43
【问题描述】:

即目前我正在向我的网格视图添加一个页脚行,如下所示

    Protected Sub gvShoppingCart_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvShoppingCart.RowDataBound
    ' If we are binding the footer row, let's add in our total
    If e.Row.RowType = DataControlRowType.Footer Then
        e.Row.Cells(5).Text = "<strong>Total Cost:</strong>"
        e.Row.Cells(6).Text = ShoppingCart.Instance.GetSubTotal().ToString("C")
    End If
End Sub

我怎样才能添加更多的页脚行,即总折扣、总节省等,如上所示

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    下面是一些基于页脚行插入新行的代码。您可以修改它以插入多行。

        Protected Sub gvShoppingCart_DataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvShoppingCart.DataBound
            Dim grid as GridView = CType(sender, GridView)
    
            ''gets the current footer row to clone
            Dim footer As GridViewRow = grid.FooterRow
            Dim numCells = footer.Cells.Count
    
            Dim newRow As New GridViewRow(footer.RowIndex + 1, -1, footer.RowType, footer.RowState)
    
            ''have to add in the right number of cells
            ''this also copies any 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(5).Text = "Total Discount:"
            newRow.Cells(6).Text = "55.00"
    
            ''add new row to the gridview table, at the very bottom
            CType(grid.Controls(0), Table).Rows.Add(newRow)
    
        End Sub
    

    【讨论】:

      【解决方案2】:

      使用默认控件,只有一个页脚行,因此,您必须手动管理任何其他项目的显示,很可能通过插入
      或类似标签来创建其他行。

      您可以为页脚中的字段创建自定义模板以帮助控制布局。

      【讨论】:

        【解决方案3】:

        你需要在&lt;asp:TemplateField&gt;&lt;FooterTemplate&gt;中使用&lt;FooterTemplate&gt;GridView

        这里是一个例子:http://csharpdotnetfreak.blogspot.com/2009/07/display-total-in-gridview-footer.html

        更好的选择是使用ListView 控件。它为布局提供了更大的灵活性。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-10-27
          • 1970-01-01
          • 2014-10-25
          • 2013-08-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-17
          相关资源
          最近更新 更多