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 エクスプローラーで使用されるグループ化に似ています。 この例では、グループは動的に作成されます。 サブ項目列ごとに、一意のサブ項目値ごとに 1 つのグループが作成されます。 親項目列の場合、一意の最初の文字ごとに 1 つのグループが作成されます。 各列に対して作成されたグループは、サブ項目テキストまたは初期文字と共にハッシュ テーブルに格納されます。 列ヘッダーをクリックすると、その列に対応するハッシュ テーブルが取得されます。 次に、その列のサブ項目テキスト値をハッシュ テーブル キーとして使用して、各項目の正しいグループを取得します。 その後、 プロパティを使用して項目がグループに 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 で、変更できません。 すべての項目がグループに適切に追加されていることを確認するために、デバッグで主に役立ちます。

Note

ListView グループは、Windows XP および Windows Server 2003 ファミリ (Windows XP Home Edition、Windows XP Professional、Windows Server 2003) でのみ使用できます。 詳細については、ListViewGroup の概要のトピックを参照してください。

適用対象

こちらもご覧ください