I would appreciate a snippet of code for adding groups at run-time to a listview. I am currently listing books (and stats) without groups via the following code which is executed in a loop (one pass per book title).

item = New ListViewItem
item.Text = seq.ToString
item.SubItems.Add(Mid(titlenode.Nodes(N_CSTAT).Text, 3))
item.SubItems.Add(Mid(titlenode.Nodes(N_JSTAT).Text, 3))
item.SubItems.Add(titlenode.Nodes(N_PUBLISHED).Text)
item.SubItems.Add(titlenode.Text)
item.SubItems.Add(titlenode.Nodes(N_SERIES_NAME).Text)
item.SubItems.Add(titlenode.Nodes(N_SERIES_INDX).Text)
item.SubItems.Add(titlenode.Nodes(N_SYNOPSIS).Text)

lvwTitles.Items.Add(item)

The listview is set to details view. What I want to do is group the book titles by series. For example, books by Michael Connelly may be grouped by "Harry Bosch", "Jack McEvoy", "Mickey Haller", etc. I can create a new group for each new series by

groupnum += 1
group = New ListViewGroup("group" & groupnum, _
        System.Windows.Forms.HorizontalAlignment.Left)
group.Name = "group" & groupnum
group.Header = series

but I cannot find how to add the group to the listview and add the new item to the associated group.

here try something like this:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim item As New ListViewItem '("Harry Potter and the Philosopher's Stone", "J K Rowling")
        item.Text = "Harry Potter and the Philosopher's Stone"
        item.SubItems.Add("J K Rowling")
        ListView1.Items.Add(item)

        item = New ListViewItem
        item.Text = "Harry Potter and the Chamber of Secrets"
        item.SubItems.Add("J K Rowling")
        ListView1.Items.Add(item)

        item = New ListViewItem
        item.Text = "Harry Potter and the Prisoner of Azkaban "
        item.SubItems.Add("J K Rowling")
        ListView1.Items.Add(item)

        item = New ListViewItem
        item.Text = "Harry Potter and the Goblet of Fire"
        item.SubItems.Add("J K Rowling")
        ListView1.Items.Add(item)

        item = New ListViewItem
        item.Text = "Harry Potter and the Order of the Phoenix"
        item.SubItems.Add("J K Rowling")
        ListView1.Items.Add(item)

        item = New ListViewItem
        item.Text = "Harry Potter and the Half-Blood Prince"
        item.SubItems.Add("J K Rowling")
        ListView1.Items.Add(item)

        item = New ListViewItem
        item.Text = "Harry Potter and the Deathly Hallows"
        item.SubItems.Add("J K Rowling")
        ListView1.Items.Add(item)

        ''

        item = New ListViewItem
        item.Text = "Capital"
        item.SubItems.Add("Karl Marx")
        ListView1.Items.Add(item)

        item = New ListViewItem
        item.Text = "Value, Price and Profit"
        item.SubItems.Add("Karl Marx")
        ListView1.Items.Add(item)

        item = New ListViewItem
        item.Text = "HManifesto of Communist Party"
        item.SubItems.Add("Karl Marx")
        ListView1.Items.Add(item)


        'Now lets say i want to group by the Author.. i am only showing 2 authors but there can be hundreds in your application.
        'Here is the automatic function which group the authors automatically

        Dim flag As Boolean = True
        For Each l As ListViewItem In ListView1.Items

            Dim strmyGroupname As String = l.SubItems(1).Text

            For Each lvg As ListViewGroup In ListView1.Groups

                If lvg.Name = strmyGroupname Then
                    l.Group = lvg
                    flag = False
                End If

            Next

            If flag = True Then
                Dim lstGrp As New ListViewGroup(strmyGroupname, strmyGroupname)
                ListView1.Groups.Add(lstGrp)
                l.Group = lstGrp
            End If

            flag = True

        Next


    End Sub
End Class

Complete Project Code is attached..

commented: Well written and aesthetically pleasing code. +3

Excellent. That's exactly what I was looking for. Took me 5 minutes to plug it into my code. It's also great to see someone who understands the importance of whitespace. Over 32 years of programming I've learned that I spend a lot more time reading and modifying code than I do writing it so I appreciate it when the code is pleasing to the eye as well as well constructed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.