Represents the collection of groups within a System.Windows.Forms.ListView control.
See Also: ListViewGroupCollection Members
Use the ListView.Groups property to get the System.Windows.Forms.ListViewGroupCollection associated with a System.Windows.Forms.ListView control. This collection contains the System.Windows.Forms.ListViewGroup objects that represent the groups shown in the control when the ListView.View property is set to a value other than View.List. Any items that are not assigned to a group will appear in the default group, which has the header label "DefaultGroup{0}". 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. If there are no groups in the ListView.Groups collection, the grouping feature is disabled.
Use the ListViewGroupCollection.Add(ListViewGroup) method to add a single group to the collection. Use the ListViewGroupCollection.Insert(int, ListViewGroup) method to add a group at a particular index within the collection. To remove a group, use the ListViewGroupCollection.Remove(ListViewGroup) method. Use the ListViewGroupCollection.RemoveAt(int) method to remove the group at a particular index.
You cannot add a System.Windows.Forms.ListViewGroup to the collection more than once. To reposition a group within the collection, it must first be removed from the collection, and then inserted at the desired location. Use the ListViewGroupCollection.Contains(ListViewGroup) method to determine whether a particular group is already in the collection. To retrieve the index of a group within the collection, use the ListViewGroupCollection.IndexOf(ListViewGroup) method. You can get or set the group at a particular index with the ListViewGroupCollection.Item(int) indexer.
Use the ListViewGroupCollection.AddRange(ListViewGroup[]) method to add multiple groups to the collection. You can add multiple groups either as an array of groups or as a System.Windows.Forms.ListViewGroupCollection that you retrieve through the ListView.Groups property of another System.Windows.Forms.ListView control. Use the ListViewGroupCollection.Clear method to remove all the groups from the collection.
The ListViewGroupCollection.Remove(ListViewGroup), ListViewGroupCollection.RemoveAt(int), and ListViewGroupCollection.Clear methods remove groups from the collection, but do not remove any items from the System.Windows.Forms.ListView control. If there are no groups in the ListView.Groups collection, the grouping feature is disabled and all items in the control are displayed normally.
The ListViewGroupCollection.AddRange(ListViewGroup[]) and ListViewGroupCollection.Clear methods are useful when you want to provide multiple ways to group the items in a System.Windows.Forms.ListView control. To do this, create multiple group arrays. To change the grouping, first use the ListViewGroupCollection.Clear method to remove all the groups from the collection, then use the ListViewGroupCollection.AddRange(ListViewGroup[]) method to add the next array of groups to display.
Use the ListViewGroupCollection.CopyTo(Array, int) method to copy the groups in a collection to a compatible array starting at a specified index. This is useful, for example, when you want to sort the groups in the collection using the erload:System.Array.Sort method. To do this, copy the groups into a compatible array, then sort the array. Next, use the ListViewGroupCollection.Clear method to remove all the groups from the collection, then use the ListViewGroupCollection.AddRange(ListViewGroup[]) method to add the sorted array back to the collection.
Use the ListViewGroupCollection.Count property to determine how many groups are in the collection. To iterate through the collection, use the IEnumerator returned from the ListViewGroupCollection.GetEnumerator method.
The grouping feature is available only on Windows XP and the Windows Server 2003 family when your application calls the Application.EnableVisualStyles method. On earlier operating systems, any code relating to groups will be ignored and the groups will not appear. As a result, any code that depends on the grouping feature might not work correctly. You might want to include a test that determines whether the grouping feature is available, and provide alternate functionality when it is unavailable. For example, you might want to provide alternate sorting when running on operating systems that do not support sorting by group.
The insertion mark feature is provided by the same library that provides the operating system themes feature. To check for the availability of this library, call the FeatureSupport.IsPresent(object) method overload and pass in the OSFeature.Themes value.