Bagikan melalui


ListViewGroupCollection.AddRange Metode

Definisi

Menambahkan beberapa grup ke koleksi.

Overload

Nama Deskripsi
AddRange(ListViewGroup[])

Menambahkan array grup ke koleksi.

AddRange(ListViewGroupCollection)

Menambahkan grup dalam koleksi yang sudah ada ListViewGroupCollection .

AddRange(ListViewGroup[])

Sumber:
ListViewGroupCollection.cs
Sumber:
ListViewGroupCollection.cs
Sumber:
ListViewGroupCollection.cs
Sumber:
ListViewGroupCollection.cs
Sumber:
ListViewGroupCollection.cs

Menambahkan array grup ke koleksi.

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())

Parameter

groups
ListViewGroup[]

Array jenis ListViewGroup yang menentukan grup yang akan ditambahkan ke koleksi.

Pengecualian

groups berisi setidaknya satu grup dengan setidaknya satu ListViewItem yang termasuk dalam kontrol selain yang memiliki ListView ini ListViewGroupCollection.

Koleksi ListView ini ditetapkan dalam mode virtual.

Contoh

Contoh berikut menunjukkan bagaimana AddRange metode dapat digunakan dalam aplikasi yang mengatur ListView item berdasarkan nilai subitem dalam tampilan detail. Bentuk pengelompokan ini mirip dengan pengelompokan yang digunakan di Windows Explorer. Dalam contoh, grup dibuat secara dinamis. Untuk setiap kolom subitem, satu grup dibuat untuk setiap nilai subitem unik. Untuk kolom item induk, satu grup dibuat untuk setiap huruf awal yang unik. Grup yang dibuat untuk setiap kolom disimpan dalam tabel hash bersama dengan teks subitem atau huruf awal. Saat header kolom diklik, dikosongkan ListViewGroupCollection . Tabel hash yang sesuai dengan kolom yang diklik kemudian diambil dan setiap item ditetapkan ke grup yang sesuai. Terakhir, array grup yang diurutkan dalam tabel hash ditambahkan ke ListViewGroupCollection.

Untuk contoh lengkapnya, lihat ListViewGroupCollection topik referensi gambaran umum.

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

Keterangan

Gunakan versi AddRange metode ini untuk menambahkan array grup ke koleksi grup. Metode ini berguna ketika Anda membuat beberapa ListViewGroup objek dan ingin menambahkannya ke koleksi dengan satu panggilan metode. Untuk menambahkan grup individual ke koleksi, gunakan Add metode .

Metode ini juga berguna ketika Anda ingin menyediakan beberapa cara untuk mengelompokkan item dalam ListView kontrol. Untuk melakukan ini, buat beberapa array grup. Untuk mengubah pengelompokan, pertama-tama Clear gunakan metode untuk menghapus semua grup dari koleksi, lalu gunakan AddRange metode untuk menambahkan array grup yang berbeda.

Tidak seperti metode , AddAddRange metode ini tidak memiliki nilai pengembalian yang dapat digunakan untuk menentukan apakah grup yang ditambahkan sudah ada dalam koleksi. Jika Anda memerlukan informasi ini, gunakan Contains metode sebelum menggunakan AddRange metode .

Lihat juga

Berlaku untuk

AddRange(ListViewGroupCollection)

Sumber:
ListViewGroupCollection.cs
Sumber:
ListViewGroupCollection.cs
Sumber:
ListViewGroupCollection.cs
Sumber:
ListViewGroupCollection.cs
Sumber:
ListViewGroupCollection.cs

Menambahkan grup dalam koleksi yang sudah ada 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)

Parameter

groups
ListViewGroupCollection

Yang ListViewGroupCollection berisi grup untuk ditambahkan ke koleksi.

Pengecualian

groups berisi setidaknya satu grup dengan setidaknya satu ListViewItem yang termasuk dalam kontrol selain yang memiliki ListView ini ListViewGroupCollection.

Koleksi ListView ini ditetapkan dalam mode virtual.

Keterangan

Gunakan versi AddRange metode ini untuk menambahkan elemen ListViewGroupCollection yang Anda ambil melalui ListView.Groups properti kontrol yang berbeda ListView .

Tidak seperti metode , AddAddRange metode ini tidak memiliki nilai pengembalian yang dapat digunakan untuk menentukan apakah grup yang ditambahkan sudah ada dalam koleksi. Jika Anda memerlukan informasi ini, gunakan Contains metode sebelum menggunakan AddRange metode .

Berlaku untuk