Export (0) Print
Expand All

ListViewItem.Group Property

Gets or sets the group to which the item is assigned.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public Property Group As ListViewGroup

Property Value

Type: System.Windows.Forms.ListViewGroup
The ListViewGroup to which the item is assigned.

Use this property to set the group to which an item belongs. You can also set the group in the ListViewItem constructor, or you can use this property to modify the group membership at run time. If you set this property to Nothing and there are groups in the ListView.Groups collection, the item will appear in the default group, which has the header label "DefaultGroupSystem.Windows.Forms". The default group is not contained in the ListView.Groups collection, and cannot be altered. It is primarily useful in debugging to ensure that all items have been properly added to groups.

NoteNote

ListView groups are only available on Windows XP and the Windows Server 2003 family (Windows XP Home Edition, Windows XP Professional, Windows Server 2003). For more information, see the ListViewGroup overview topic.

The following code example demonstrates how the Group property can be used in an application that organizes ListView items by subitem value in the details view. This form of grouping is similar to the grouping used in Windows Explorer. In the example, the groups are created dynamically. For each subitem column, one group is created for each unique subitem value. For the parent item column, one group is created for each unique initial letter. The groups created for each column are stored in a hash table along with the subitem text or initial letter. When a column header is clicked, the hash table corresponding to that column is retrieved. Next, the subitem text values for that column are used as hash table keys to retrieve the correct group for each item. The item is then assigned to the group using the Group property.

This code example is part of a larger example provided for the ListView.Groups property.

' Sets myListView to the groups created for the specified column. 
Private Sub SetGroups(column As Integer)
    ' Remove the current groups.
    myListView.Groups.Clear()

    ' Retrieve the hash table corresponding to the column. 
    Dim groups As Hashtable = CType(groupTables(column), Hashtable)

    ' Copy the groups for the column to an array. 
    Dim groupsArray(groups.Count - 1) As ListViewGroup
    groups.Values.CopyTo(groupsArray, 0)

    ' Sort the groups and add them to myListView.
    Array.Sort(groupsArray, New ListViewGroupSorter(myListView.Sorting))
    myListView.Groups.AddRange(groupsArray)

    ' Iterate through the items in myListView, assigning each  
    ' one to the appropriate group. 
    Dim item As ListViewItem
    For Each item In myListView.Items
        ' Retrieve the subitem text corresponding to the column. 
        Dim subItemText As String = item.SubItems(column).Text

        ' For the Title column, use only the first letter. 
        If column = 0 Then
            subItemText = subItemText.Substring(0, 1)
        End If  

        ' Assign the item to the matching group.
        item.Group = CType(groups(subItemText), ListViewGroup)
    Next item
End Sub 'SetGroups

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft