Export (0) Print
Expand All

ListViewGroupCollection.Clear Method

Removes all groups from the collection.

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

public void Clear()



Use this method to remove all the groups from the collection. Note that removing groups from the ListView.Groups collection does not remove items from the ListView control.

This method is useful to disable the grouping feature. When there are no groups in a ListView control, the items appear normally. To remove individual groups from the collection, use the Remove or RemoveAt method.

This method is also useful when you want to provide multiple ways to group the items. To change the grouping, first use the Clear method to remove all the groups from the collection, then use the AddRange method to add a different array of groups.

The following example demonstrates how the Clear method 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 ListViewGroupCollection is cleared. The hash table corresponding to the clicked column is then retrieved and each item is assigned to the appropriate group. Finally, a sorted array of the groups in the hash table is added to the ListViewGroupCollection.

For the complete example, see the ListViewGroupCollection overview reference topic.

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

    // 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));

    // 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];

.NET Framework

Supported in: 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.

© 2014 Microsoft