在ASP.Net ListView中对行进行分组

时间:2009-08-25 00:33:11

标签: asp.net listview grouping

我是ASP.NET新手。我试图使用列表视图显示我的SQL结果。我正在使用该示例通过4GuysFromRolla.com website中的数据字段对结果进行分组。但是,我发现通过数据字段对项目进行分组的方式有点笨拙。有没有更好的方法呢?

感谢。

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

我从未使用过ListView,但我确实在GridView中进行了分组。如果需要,可以尝试将其移植到ListView:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    Dim tblGrid As Table = Me.GridView1.Controls(0)
    Dim strLastCat As String = "@"
    Dim row As GridViewRow


    For Each row In GridView1.Rows
        Dim intRealIndex As Integer = tblGrid.Rows.GetRowIndex(row)
        Dim strCat As String = Me.GridView1.DataKeys(row.RowIndex).Value

        If strLastCat <> strCat Then
            Dim rowHeader As New GridViewRow(intRealIndex, intRealIndex, DataControlRowType.Separator, DataControlRowState.Normal)
            Dim newCell As New TableCell

            newCell.ColumnSpan = Me.GridView1.Columns.Count
            newCell.BackColor = System.Drawing.Color.FromArgb(61, 138, 20)
            newCell.ForeColor = System.Drawing.Color.FromArgb(255, 255, 255)
            newCell.Font.Bold = True
            newCell.Font.Size = New FontUnit(FontSize.Larger)
            newCell.Text = strCat

            rowHeader.Cells.Add(newCell)
            tblGrid.Controls.AddAt(intRealIndex, rowHeader)
            strLastCat = strCat

        End If

    Next

    MyBase.Render(writer)
End Sub

代码为每个类别创建标题。最终版本可在此处查看:http://www.truedietreviews.com/diet-reviews/