ListViewItem.Group Property

Definition

获取或设置该项所分配到的组。Gets or sets the group to which the item is assigned.

public:
 property System::Windows::Forms::ListViewGroup ^ Group { System::Windows::Forms::ListViewGroup ^ get(); void set(System::Windows::Forms::ListViewGroup ^ value); };
public System.Windows.Forms.ListViewGroup Group { get; set; }
member this.Group : System.Windows.Forms.ListViewGroup with get, set
Public Property Group As ListViewGroup

Property Value

ListViewGroup

该项所分配到的 ListViewGroupThe ListViewGroup to which the item is assigned.

Examples

下面的代码示例演示如何在应用程序中使用 Group 属性,以便在详细信息视图中按子项值组织 ListView 项。The following code example demonstrates how the Group property can be used in an application that organizes ListView items by subitem value in the details view. 这种形式的分组类似于 Windows 资源管理器中使用的分组。This form of grouping is similar to the grouping used in Windows Explorer. 在此示例中,将动态创建组。In the example, the groups are created dynamically. 对于每个子项列,将为每个唯一子项值创建一个组。For each subitem column, one group is created for each unique subitem value. 对于 "父项" 列,将为每个唯一的初始字母创建一个组。For the parent item column, one group is created for each unique initial letter. 为每个列创建的组与子项文本或首字母一起存储在哈希表中。The groups created for each column are stored in a hash table along with the subitem text or initial letter. 单击列标题时,将检索与该列对应的哈希表。When a column header is clicked, the hash table corresponding to that column is retrieved. 接下来,该列的子项文本值用作哈希表键,以便为每个项检索正确的组。Next, the subitem text values for that column are used as hash table keys to retrieve the correct group for each item. 然后,使用 Group 属性将项分配给组。The item is then assigned to the group using the Group property.

此代码示例是为 ListView.Groups 属性提供的更大示例的一部分。This code example is part of a larger example provided for the ListView.Groups property.

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

Remarks

使用此属性可设置项所属的组。Use this property to set the group to which an item belongs. 你还可以在 ListViewItem 构造函数中设置组,也可以使用此属性在运行时修改组成员身份。You can also set the group in the ListViewItem constructor, or you can use this property to modify the group membership at run time. 如果将此属性设置为 null 并且 ListView.Groups 集合中有组,则该项将出现在具有标头标签 "DefaultGroupSystem" 的默认组中。If you set this property to null and there are groups in the ListView.Groups collection, the item will appear in the default group, which has the header label "DefaultGroupSystem.Windows.Forms". 默认组不包含在 ListView.Groups 集合中,因此无法更改。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.

Note

ListView 组仅适用于 Windows XP 和 Windows Server 2003 家族(Windows XP Home Edition、Windows XP Professional、Windows Server 2003Windows XP Home Edition, Windows XP Professional, Windows Server 2003)。ListView groups are only available on Windows XP and the Windows Server 2003 family (Windows XP Home Edition、Windows XP Professional、Windows Server 2003Windows XP Home Edition, Windows XP Professional, Windows Server 2003). 有关更多信息,请参见 ListViewGroup 概述主题。For more information, see the ListViewGroup overview topic.

Applies to

See also