ListViewItem.Group 屬性

定義

取得或設定指派此項目至其中的群組。

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; }
public System.Windows.Forms.ListViewGroup? Group { get; set; }
member this.Group : System.Windows.Forms.ListViewGroup with get, set
Public Property Group As ListViewGroup

屬性值

指派此項目至其中的 ListViewGroup

範例

下列程式碼範例示範如何在 Group 應用程式中使用 屬性,以依據詳細資料檢視中的子專案值來組織 ListView 專案。 這種群組形式類似于 Windows 檔案總管中使用的群組。 在此範例中,會動態建立群組。 針對每個子專案資料行,會為每個唯一子專案值建立一個群組。 針對父專案資料行,會為每個唯一的初始字母建立一個群組。 針對每個資料行建立的群組會儲存在雜湊表中,以及子專案文字或初始字母。 按一下資料行標頭時,會擷取對應至該資料行的雜湊表。 接下來,該資料行的子專案文字值會用來做為雜湊表索引鍵,以擷取每個專案的正確群組。 然後,專案會使用 Group 屬性指派給群組。

此程式碼範例是提供給 屬性之較大範例的 ListView.Groups 一部分。

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

備註

使用此屬性可設定專案所屬的群組。 您也可以在建構函式中 ListViewItem 設定群組,也可以使用這個屬性在執行時間修改群組成員資格。 如果您將此屬性設定為 null ,而且集合中有 ListView.Groups 群組,則專案會出現在預設群組中,其中包含標頭標籤 「DefaultGroupSystem.Windows.Forms」。 預設群組不包含在集合中 ListView.Groups ,而且無法改變。 它主要用於偵錯,以確保所有專案都已正確新增至群組。

注意

ListView 群組僅適用于 Windows XP 和 Windows Server 2003 系列 (Windows XP Home Edition、Windows XP Professional、Windows Server 2003) 。 如需詳細資訊,請參閱 ListViewGroup 概觀主題。

適用於

另請參閱