views:

1277

answers:

2

I am new to ASP.NET. I am trying to display my sql results using a list view. I am using the example for grouping my results by a data field from the 4GuysFromRolla.com website. However, I find the way of grouping the items by a data field to be a bit clumsy. Is there a better way to do it?

Thanks.

+2  A: 

Nested ListView - http://mattberseth.com/blog/2008/01/building_a_grouping_grid_with.html

adatapost
I was unsure with using Linq to SQL, but once I added the relationships to the dbml, It worked like a charm. Thanks.
daub815
A: 

I have never used a ListView, but I did do grouping in a GridView. You can try porting this over to a ListView if you want:

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

The code creates headers each category. The final version can be viewed here: http://www.truedietreviews.com/diet-reviews/

Martin