ListViewGroupCollection.AddRange 方法

定義

將多個群組加入至集合。

多載

AddRange(ListViewGroup[])

將群組陣列加入至集合。

AddRange(ListViewGroupCollection)

將現有 ListViewGroupCollection 中的群組加入集合中。

AddRange(ListViewGroup[])

將群組陣列加入至集合。

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

參數

groups
ListViewGroup[]

ListViewGroup 型別的陣列,指定要加入至集合的群組。

例外狀況

groups 至少包含一個群組,其中至少有一個 ListViewItem 屬於不是擁有此 ListViewListViewGroupCollection 控制項。

ListView指派給這個集合的 處於虛擬模式。

範例

下列範例示範如何在 AddRange 依詳細資料檢視中的子專案值來組織 ListView 專案的應用程式中使用 方法。 這種群組形式類似于 Windows 檔案總管中使用的群組。 在此範例中,會動態建立群組。 針對每個子專案資料行,會為每個唯一子專案值建立一個群組。 針對父專案資料行,會為每個唯一的初始字母建立一個群組。 針對每個資料行建立的群組會儲存在雜湊表中,以及子專案文字或初始字母。 按一下資料行標頭時,會 ListViewGroupCollection 清除 。 接著會擷取對應至所按一下資料行的雜湊表,並將每個專案指派給適當的群組。 最後,雜湊表中群組的已排序陣列會新增至 ListViewGroupCollection

如需完整的範例,請參閱 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

備註

使用這個版本的 AddRange 方法,將群組陣列新增至群組集合。 當您建立多個 ListViewGroup 物件,並想要使用單一方法呼叫將其新增至集合時,這個方法很有用。 若要將個別群組新增至集合,請使用 Add 方法。

當您想要提供多種方式來將控制項中的 ListView 專案分組時,此方法也很有用。 若要這樣做,請建立多個群組陣列。 若要變更群組,請先使用 Clear 方法從集合中移除所有群組,然後使用 AddRange 方法新增不同的群組陣列。

Add不同于 方法, AddRange 方法沒有傳回值,可用來判斷要加入的群組是否已經在集合中。 如果您需要此資訊,請先使用 Contains 方法,再使用 AddRange 方法。

另請參閱

適用於

AddRange(ListViewGroupCollection)

將現有 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)

參數

groups
ListViewGroupCollection

ListViewGroupCollection,含有要加入至集合的群組。

例外狀況

groups 至少包含一個群組,其中至少有一個 ListViewItem 屬於不是擁有此 ListViewListViewGroupCollection 控制項。

ListView指派給這個集合的 處於虛擬模式。

備註

使用這個版本的 AddRange 方法,新增透過不同 ListView 控制項的 屬性擷取 ListView.GroupsListViewGroupCollection 元素。

Add不同于 方法, AddRange 方法沒有傳回值,可用來判斷要加入的群組是否已經在集合中。 如果您需要此資訊,請先使用 Contains 方法,再使用 AddRange 方法。

適用於