Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ListViewItem.Group Property

Note: This property is new in the .NET Framework version 2.0.

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

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

[LocalizableAttribute(true)] 
public ListViewGroup Group { get; set; }
/** @property */
public ListViewGroup get_Group ()

/** @property */
public void set_Group (ListViewGroup value)

public function get Group () : ListViewGroup

public function set Group (value : ListViewGroup)

Property Value

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 a null reference (Nothing in Visual Basic) 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 " 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 ", 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 " is part of a larger " provided for the ListView.Groups property.

// Sets myListView to the groups created for the specified column.
private void SetGroups(int column)
{
    // Remove the current groups.
    myListView.Groups.Clear();

    // Retrieve the hash table corresponding to the column.
    Hashtable groups = (Hashtable)groupTables[column];

    // Copy the groups for the column to an array.
    ListViewGroup[] groupsArray = new ListViewGroup[groups.Count];
    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.
    foreach (ListViewItem item in myListView.Items)
    {
        // Retrieve the subitem text corresponding to the column.
        string subItemText = item.SubItems[column].Text;

        // For the Title column, use only the first letter.
        if (column == 0) 
        {
            subItemText = subItemText.Substring(0, 1);
        }

        // Assign the item to the matching group.
        item.Group = (ListViewGroup)groups[subItemText];
    }
}

// Sets myListView to the groups created for the specified column.
private void SetGroups(int column)
{
    // Remove the current groups.
    myListView.get_Groups().Clear();
    // Retrieve the hash table corresponding to the column.
    Hashtable groups = (Hashtable)groupTables.get_Item(column);
    // Iterate through the items in myListView, assigning each 
    // one to the appropriate group.
    for (int iCtr = 0; iCtr < myListView.get_Items().get_Count(); iCtr++) {
        ListViewItem item = myListView.get_Items().get_Item(iCtr);
        // Retrieve the subitem text corresponding to the column.
        String subItemText = item.get_SubItems().get_Item(column).get_Text();
        // For the Title column, use only the first letter.
        if (column == 0) {
            subItemText = subItemText.Substring(0, 1);
        }
        // Assign the item to the matching group.
        item.set_Group((ListViewGroup)(groups.get_Item(subItemText)));
    }
    // Copy the groups for the column to an array.
    ListViewGroup groupsArray[] = new ListViewGroup[groups.get_Count()];
    groups.get_Values().CopyTo(groupsArray, 0);
    // Sort the groups and add them to myListView.
    Array.Sort(groupsArray, new ListViewGroupSorter(
        myListView.get_Sorting()));
    myListView.get_Groups().AddRange(groupsArray);
} //SetGroups

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.