ListViewGroupCollection.AddRange 方法

定义

将多个组添加到集合。

重载

AddRange(ListViewGroup[])

向集合中添加组的数组。

AddRange(ListViewGroupCollection)

向集合中添加现有 ListViewGroupCollection 中的组。

AddRange(ListViewGroup[])

向集合中添加组的数组。

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

参数

groups
ListViewGroup[]

一个 ListViewGroup 类型的数组,指定要添加到集合中的组。

例外

groups 至少包含一个组,该组至少有一个 ListViewItem 属于 ListView 控件,而该控件不是拥有此 ListViewGroupCollection 的控件。

分配给 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 属于 ListView 控件,而该控件不是拥有此 ListViewGroupCollection 的控件。

分配给 ListView 此集合的值为虚拟模式。

注解

使用此版本的AddRange方法可以添加通过ListView.Groups其他ListView控件的属性检索的元素ListViewGroupCollection

Add与该方法不同,该方法AddRange没有可用于确定要添加的组是否已在集合中的返回值。 如果需要此信息,请使用 Contains 该方法之前使用 AddRange 该方法。

适用于