Partager via


ListViewGroupCollection.AddRange Méthode

Définition

Ajoute plusieurs groupes à la collection.

Surcharges

Nom Description
AddRange(ListViewGroup[])

Ajoute un tableau de groupes à la collection.

AddRange(ListViewGroupCollection)

Ajoute les groupes d’une collection existante ListViewGroupCollection .

AddRange(ListViewGroup[])

Source:
ListViewGroupCollection.cs
Source:
ListViewGroupCollection.cs
Source:
ListViewGroupCollection.cs
Source:
ListViewGroupCollection.cs
Source:
ListViewGroupCollection.cs

Ajoute un tableau de groupes à la collection.

public:
 void AddRange(cli::array <System::Windows::Forms::ListViewGroup ^> ^ groups);
public:
 void AddRange(... cli::array <System::Windows::Forms::ListViewGroup ^> ^ groups);
public void AddRange(System.Windows.Forms.ListViewGroup[] groups);
public void AddRange(params System.Windows.Forms.ListViewGroup[] groups);
member this.AddRange : System.Windows.Forms.ListViewGroup[] -> unit
Public Sub AddRange (groups As ListViewGroup())
Public Sub AddRange (ParamArray groups As ListViewGroup())

Paramètres

groups
ListViewGroup[]

Tableau de type ListViewGroup qui spécifie les groupes à ajouter à la collection.

Exceptions

groups contient au moins un groupe avec au moins un ListViewItem qui appartient à un ListView contrôle autre que celui qui possède cette ListViewGroupCollectionpropriété .

La ListView collection à laquelle cette collection est affectée est en mode virtuel.

Exemples

L’exemple suivant montre comment la AddRange méthode peut être utilisée dans une application qui organise les ListView éléments par valeur sous-élément dans la vue détails. Cette forme de regroupement est similaire au regroupement utilisé dans l’Explorateur Windows. Dans l’exemple, les groupes sont créés dynamiquement. Pour chaque colonne sous-élément, un groupe est créé pour chaque valeur sous-élément unique. Pour la colonne d’élément parent, un groupe est créé pour chaque lettre initiale unique. Les groupes créés pour chaque colonne sont stockés dans une table de hachage, ainsi que le texte sous-élément ou la lettre initiale. Lorsqu’un en-tête de colonne est cliqué, l’en-tête ListViewGroupCollection de colonne est effacé. La table de hachage correspondant à la colonne cliquée est ensuite récupérée et chaque élément est affecté au groupe approprié. Enfin, un tableau trié des groupes dans la table de hachage est ajouté au ListViewGroupCollection.

Pour obtenir l’exemple complet, consultez la rubrique de référence de vue d’ensemble ListViewGroupCollection .

   // 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 = dynamic_cast<Hashtable^>(groupTables[column]);

      // Copy the groups for the column to an array.
      array<ListViewGroup^>^ groupsArray = gcnew array<ListViewGroup^>(groups->Count);
      groups->Values->CopyTo(groupsArray, 0);

      // Sort the groups and add them to myListView.
      Array::Sort(groupsArray, gcnew ListViewGroupSorter(myListView->Sorting));
      myListView->Groups->AddRange(groupsArray);

      // Iterate through the items in myListView, assigning each 
      // one to the appropriate group.
      IEnumerator^ myEnum = myListView->Items->GetEnumerator();
      while (myEnum->MoveNext())
      {
         ListViewItem^ item = safe_cast<ListViewItem^>(myEnum->Current);
         // 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 = dynamic_cast<ListViewGroup^>(groups[subItemText]);
      }
   }
// 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 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

Remarques

Utilisez cette version de la AddRange méthode pour ajouter un tableau de groupes à la collection de groupes. Cette méthode est utile lorsque vous créez plusieurs ListViewGroup objets et souhaitez les ajouter à la collection avec un seul appel de méthode. Pour ajouter des groupes individuels à la collection, utilisez la Add méthode.

Cette méthode est également utile lorsque vous souhaitez fournir plusieurs façons de regrouper les éléments dans un ListView contrôle. Pour ce faire, créez plusieurs tableaux de groupes. Pour modifier le regroupement, utilisez d’abord la Clear méthode pour supprimer tous les groupes de la collection, puis utilisez la AddRange méthode pour ajouter un autre tableau de groupes.

Contrairement à la Add méthode, la AddRange méthode n’a pas de valeur de retour qui peut être utilisée pour déterminer si un groupe ajouté est déjà dans la collection. Si vous avez besoin de ces informations, utilisez la Contains méthode avant d’utiliser la AddRange méthode.

Voir aussi

S’applique à

AddRange(ListViewGroupCollection)

Source:
ListViewGroupCollection.cs
Source:
ListViewGroupCollection.cs
Source:
ListViewGroupCollection.cs
Source:
ListViewGroupCollection.cs
Source:
ListViewGroupCollection.cs

Ajoute les groupes d’une collection existante ListViewGroupCollection .

public:
 void AddRange(System::Windows::Forms::ListViewGroupCollection ^ groups);
public void AddRange(System.Windows.Forms.ListViewGroupCollection groups);
member this.AddRange : System.Windows.Forms.ListViewGroupCollection -> unit
Public Sub AddRange (groups As ListViewGroupCollection)

Paramètres

groups
ListViewGroupCollection

Contenant ListViewGroupCollection les groupes à ajouter à la collection.

Exceptions

groups contient au moins un groupe avec au moins un ListViewItem qui appartient à un ListView contrôle autre que celui qui possède cette ListViewGroupCollectionpropriété .

La ListView collection à laquelle cette collection est affectée est en mode virtuel.

Remarques

Utilisez cette version de la AddRange méthode pour ajouter les éléments d’un ListViewGroupCollection contrôle que vous récupérez via la ListView.Groups propriété d’un autre ListView contrôle.

Contrairement à la Add méthode, la AddRange méthode n’a pas de valeur de retour qui peut être utilisée pour déterminer si un groupe ajouté est déjà dans la collection. Si vous avez besoin de ces informations, utilisez la Contains méthode avant d’utiliser la AddRange méthode.

S’applique à