ListViewGroupCollection ListViewGroupCollection ListViewGroupCollection ListViewGroupCollection Class

定義

ListView コントロール内のグループのコレクションを表します。Represents the collection of groups within a ListView control.

public ref class ListViewGroupCollection : System::Collections::IList
[System.ComponentModel.ListBindable(false)]
public class ListViewGroupCollection : System.Collections.IList
type ListViewGroupCollection = class
    interface IList
    interface ICollection
    interface IEnumerable
Public Class ListViewGroupCollection
Implements IList
継承
ListViewGroupCollectionListViewGroupCollectionListViewGroupCollectionListViewGroupCollection
属性
実装

次の例では、グループ化ListView機能を使用して、詳細ビューのアイテムをサブ項目の値で整理する方法を示します。The following example demonstrates how to use the ListView grouping feature to organize 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. 各サブ項目列に対して、一意のサブアイテム値ごとに1つのグループが作成されます。For each subitem column, one group is created for each unique subitem value. 親アイテムの列に対しては、一意の最初の文字ごとに1つのグループが作成されます。For the parent item column, one group is created for each unique initial letter. 列のヘッダーをクリックすると、その列に対して作成されたグループに項目が並べ替えられます。Clicking the header of a column sorts the items into the groups created for that column. 同じ列ヘッダーを再度クリックすると、グループの順序が反転されます。Clicking the same column header again reverses the order of the groups.

#using <mscorlib.dll>
#using <System.Drawing.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Collections; 
using namespace System::Windows::Forms;

public ref class ListViewGroupsExample : public Form
{
private:
   ListView^ myListView;
   bool isRunningXPOrLater;

   // Declare a Hashtable array in which to store the groups.
   array<Hashtable^>^ groupTables;

   // Declare a variable to store the current grouping column.
   int groupColumn;

public:
   ListViewGroupsExample()
   {
      groupColumn = 0;
      // Initialize myListView.
      myListView = gcnew ListView();
      myListView->Dock = DockStyle::Fill;
      myListView->View = View::Details;
      myListView->Sorting = SortOrder::Ascending;

      // Create and initialize column headers for myListView.
      ColumnHeader^ columnHeader0 = gcnew ColumnHeader();
      columnHeader0->Text = "Title";
      columnHeader0->Width = -1;
      ColumnHeader^ columnHeader1 = gcnew ColumnHeader();
      columnHeader1->Text = "Author";
      columnHeader1->Width = -1;
      ColumnHeader^ columnHeader2 = gcnew ColumnHeader();
      columnHeader2->Text = "Year";
      columnHeader2->Width = -1;

      // Add the column headers to myListView.

      array<ColumnHeader^>^ temp0 = {columnHeader0, columnHeader1, columnHeader2};
      myListView->Columns->AddRange(temp0);

      // Add a handler for the ColumnClick event.
      myListView->ColumnClick += 
         gcnew ColumnClickEventHandler(this, &ListViewGroupsExample::myListView_ColumnClick);

      // Create items and add them to myListView.

      array<String^>^ temp1 = {"Programming Windows", "Petzold, Charles", "1998"};
      ListViewItem^ item0 = gcnew ListViewItem( temp1 );

      array<String^>^ temp2 = {"Code: The Hidden Language of Computer Hardware and Software",
         "Petzold, Charles", "2000"};
      ListViewItem^ item1 = gcnew ListViewItem( temp2 );

      array<String^>^ temp3 = {"Programming Windows with C#", "Petzold, Charles", "2001"};
      ListViewItem^ item2 = gcnew ListViewItem( temp3 );

      array<String^>^ temp4 = {"Coding Techniques for Microsoft Visual Basic .NET",
         "Connell, John", "2001"};
      ListViewItem^ item3 = gcnew ListViewItem( temp4 );

      array<String^>^ temp5 = {"C# for Java Developers", "Jones, Allen & Freeman, Adam",
         "2002"};
      ListViewItem^ item4 = gcnew ListViewItem( temp5 );

      array<String^>^ temp6 = {"Microsoft .NET XML Web Services Step by Step",
         "Jones, Allen & Freeman, Adam", "2002"};
      ListViewItem^ item5 = gcnew ListViewItem( temp6 );

      array<ListViewItem^>^ temp7 = {item0, item1, item2, item3, item4, item5};
      myListView->Items->AddRange( temp7 );

      // Determine whether Windows XP or a later 
      // operating system is present.
      isRunningXPOrLater = false;

      if (System::Environment::OSVersion->Version->Major > 5 ||
         ( System::Environment::OSVersion->Version->Major == 5 &&
         System::Environment::OSVersion->Version->Minor >= 1) )
      {
         isRunningXPOrLater = true;
      }

      if (isRunningXPOrLater)
      {
         // Create the groupsTable array and populate it with one 
         // hash table for each column.
         groupTables = gcnew array<Hashtable^>(myListView->Columns->Count);
         for (int column = 0; column < myListView->Columns->Count; column++)
         {
            // Create a hash table containing all the groups 
            // needed for a single column.
            groupTables[column] = CreateGroupsTable(column);
         }

         // Start with the groups created for the Title column.
         SetGroups(0);
      }

      // Initialize the form.
      this->Controls->Add(myListView);
      this->Size = System::Drawing::Size(550, 330);
      this->Text = "ListView Groups Example";
   }

   // Groups the items using the groups created for the clicked 
   // column.
private:
   void myListView_ColumnClick(
      Object^ /*sender*/, ColumnClickEventArgs^ e)
   {
      // Set the sort order to ascending when changing
      // column groups; otherwise, reverse the sort order.
      if ( myListView->Sorting == SortOrder::Descending || 
         ( isRunningXPOrLater && (e->Column != groupColumn) ) )
      {
         myListView->Sorting = SortOrder::Ascending;
      }
      else 
      {
         myListView->Sorting = SortOrder::Descending;
      }
      groupColumn = e->Column;

      // Set the groups to those created for the clicked column.
      if (isRunningXPOrLater)
      {
         SetGroups(e->Column);
      }
   }

   // 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]);
      }
   }

   // Creates a Hashtable object with one entry for each unique
   // subitem value (or initial letter for the parent item)
   // in the specified column.
private:
   Hashtable^ CreateGroupsTable(int column)
   {
      // Create a Hashtable object.
      Hashtable^ groups = gcnew Hashtable();

      // Iterate through the items in myListView.
      IEnumerator^ myEnum1 = myListView->Items->GetEnumerator();
      while (myEnum1->MoveNext())
      {
         ListViewItem^ item = safe_cast<ListViewItem^>(myEnum1->Current);
         // Retrieve the text value for the column.
         String^ subItemText = item->SubItems[column]->Text;

         // Use the initial letter instead if it is the first column.
         if (column == 0) 
         {
            subItemText = subItemText->Substring(0, 1);
         }

         // If the groups table does not already contain a group
         // for the subItemText value, add a new group using the 
         // subItemText value for the group header and Hashtable key.
         if (!groups->Contains(subItemText))
         {
            groups->Add( subItemText, gcnew ListViewGroup(subItemText, 
               HorizontalAlignment::Left) );
         }
      }

      // Return the Hashtable object.
      return groups;
   }

   // Sorts ListViewGroup objects by header value.
   ref class ListViewGroupSorter : public IComparer
   {
   private:
      SortOrder order;

      // Stores the sort order.
   public:
      ListViewGroupSorter(SortOrder theOrder) 
      { 
         order = theOrder;
      }

      // Compares the groups by header value, using the saved sort
      // order to return the correct value.
      virtual int Compare(Object^ x, Object^ y)
      {
         int result = String::Compare(
            (dynamic_cast<ListViewGroup^>(x))->Header,
            (dynamic_cast<ListViewGroup^>(y))->Header
            );
         if (order == SortOrder::Ascending)
         {
            return result;
         }
         else 
         {
            return -result;
         }
      }
   };
};

[STAThread]
int main() 
{
   Application::EnableVisualStyles();
   Application::Run(gcnew ListViewGroupsExample());
}
using System;
using System.Collections; 
using System.Windows.Forms;

public class ListViewGroupsExample : Form
{
    private ListView myListView;

    // Determine whether Windows XP or a later
    // operating system is present.
    private bool isRunningXPOrLater = 
        OSFeature.Feature.IsPresent(OSFeature.Themes);

    // Declare a Hashtable array in which to store the groups.
    private Hashtable[] groupTables;

    // Declare a variable to store the current grouping column.
    int groupColumn = 0;

    public ListViewGroupsExample()
    {
        // Initialize myListView.
        myListView = new ListView();
        myListView.Dock = DockStyle.Fill;
        myListView.View = View.Details;
        myListView.Sorting = SortOrder.Ascending;

        // Create and initialize column headers for myListView.
        ColumnHeader columnHeader0 = new ColumnHeader();
        columnHeader0.Text = "Title";
        columnHeader0.Width = -1;
        ColumnHeader columnHeader1 = new ColumnHeader();
        columnHeader1.Text = "Author";
        columnHeader1.Width = -1;
        ColumnHeader columnHeader2 = new ColumnHeader();
        columnHeader2.Text = "Year";
        columnHeader2.Width = -1;

        // Add the column headers to myListView.
        myListView.Columns.AddRange(new ColumnHeader[] 
            {columnHeader0, columnHeader1, columnHeader2});

        // Add a handler for the ColumnClick event.
        myListView.ColumnClick += 
            new ColumnClickEventHandler(myListView_ColumnClick);

        // Create items and add them to myListView.
        ListViewItem item0 = new ListViewItem( new string[] 
            {"Programming Windows", 
            "Petzold, Charles", 
            "1998"} );
        ListViewItem item1 = new ListViewItem( new string[] 
            {"Code: The Hidden Language of Computer Hardware and Software", 
            "Petzold, Charles", 
            "2000"} );
        ListViewItem item2 = new ListViewItem( new string[] 
            {"Programming Windows with C#", 
            "Petzold, Charles", 
            "2001"} );
        ListViewItem item3 = new ListViewItem( new string[] 
            {"Coding Techniques for Microsoft Visual Basic .NET", 
            "Connell, John", 
            "2001"} );
        ListViewItem item4 = new ListViewItem( new string[] 
            {"C# for Java Developers", 
            "Jones, Allen & Freeman, Adam", 
            "2002"} );
        ListViewItem item5 = new ListViewItem( new string[] 
            {"Microsoft .NET XML Web Services Step by Step", 
            "Jones, Allen & Freeman, Adam", 
            "2002"} );
        myListView.Items.AddRange(
            new ListViewItem[] {item0, item1, item2, item3, item4, item5});

        if (isRunningXPOrLater)
        {
            // Create the groupsTable array and populate it with one 
            // hash table for each column.
            groupTables = new Hashtable[myListView.Columns.Count];
            for (int column = 0; column < myListView.Columns.Count; column++)
            {
                // Create a hash table containing all the groups 
                // needed for a single column.
                groupTables[column] = CreateGroupsTable(column);
            }

            // Start with the groups created for the Title column.
            SetGroups(0);
        }

        // Initialize the form.
        this.Controls.Add(myListView);
        this.Size = new System.Drawing.Size(550, 330);
        this.Text = "ListView Groups Example";
    }

    [STAThread]
    static void Main() 
    {
        Application.EnableVisualStyles();
        Application.Run(new ListViewGroupsExample());
    }

    // Groups the items using the groups created for the clicked 
    // column.
    private void myListView_ColumnClick(
        object sender, ColumnClickEventArgs e)
    {
        // Set the sort order to ascending when changing
        // column groups; otherwise, reverse the sort order.
        if ( myListView.Sorting == SortOrder.Descending || 
            ( isRunningXPOrLater && (e.Column != groupColumn) ) )
        {
            myListView.Sorting = SortOrder.Ascending;
        }
        else 
        {
            myListView.Sorting = SortOrder.Descending;
        }
        groupColumn = e.Column;

        // Set the groups to those created for the clicked column.
        if (isRunningXPOrLater)
        {
            SetGroups(e.Column);
        }
    }

    // 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];
        }
    }

    // Creates a Hashtable object with one entry for each unique
    // subitem value (or initial letter for the parent item)
    // in the specified column.
    private Hashtable CreateGroupsTable(int column)
    {
        // Create a Hashtable object.
        Hashtable groups = new Hashtable();

        // Iterate through the items in myListView.
        foreach (ListViewItem item in myListView.Items)
        {
            // Retrieve the text value for the column.
            string subItemText = item.SubItems[column].Text;

            // Use the initial letter instead if it is the first column.
            if (column == 0) 
            {
                subItemText = subItemText.Substring(0, 1);
            }

            // If the groups table does not already contain a group
            // for the subItemText value, add a new group using the 
            // subItemText value for the group header and Hashtable key.
            if (!groups.Contains(subItemText))
            {
                groups.Add( subItemText, new ListViewGroup(subItemText, 
                    HorizontalAlignment.Left) );
            }
        }

        // Return the Hashtable object.
        return groups;
    }

    // Sorts ListViewGroup objects by header value.
    private class ListViewGroupSorter : IComparer
    {
        private SortOrder order;

        // Stores the sort order.
        public ListViewGroupSorter(SortOrder theOrder) 
        { 
            order = theOrder;
        }

        // Compares the groups by header value, using the saved sort
        // order to return the correct value.
        public int Compare(object x, object y)
        {
            int result = String.Compare(
                ((ListViewGroup)x).Header,
                ((ListViewGroup)y).Header
            );
            if (order == SortOrder.Ascending)
            {
                return result;
            }
            else 
            {
                return -result;
            }
        }
    }

}
Imports System.Collections
Imports System.Windows.Forms

Public Class ListViewGroupsExample
    Inherits Form

    Private myListView As ListView

    ' Determine whether Windows XP or a later
    ' operating system is present.
    Private isRunningXPOrLater As Boolean = _
        OSFeature.Feature.IsPresent(OSFeature.Themes)
    
    ' Declare a Hashtable array in which to store the groups.
    Private groupTables() As Hashtable
    
    ' Declare a variable to store the current grouping column.
    Private groupColumn As Integer = 0
    
    Public Sub New()
        ' Initialize myListView.
        myListView = New ListView()
        myListView.Dock = DockStyle.Fill
        myListView.View = View.Details
        myListView.Sorting = SortOrder.Ascending
        
        ' Create and initialize column headers for myListView.
        Dim columnHeader0 As New ColumnHeader()
        columnHeader0.Text = "Title"
        columnHeader0.Width = -1
        Dim columnHeader1 As New ColumnHeader()
        columnHeader1.Text = "Author"
        columnHeader1.Width = -1
        Dim columnHeader2 As New ColumnHeader()
        columnHeader2.Text = "Year"
        columnHeader2.Width = -1
        
        ' Add the column headers to myListView.
        myListView.Columns.AddRange( New ColumnHeader() _
            {columnHeader0, columnHeader1, columnHeader2} )
        
        ' Add a handler for the ColumnClick event.
        AddHandler myListView.ColumnClick, AddressOf myListView_ColumnClick
        
        ' Create items and add them to myListView.
        Dim item0 As New ListViewItem( New String() _
            {"Programming Windows", _
            "Petzold, Charles", _
            "1998"} )
        Dim item1 As New ListViewItem( New String() _
            {"Code: The Hidden Language of Computer Hardware and Software", _
            "Petzold, Charles", _
            "2000"} )
        Dim item2 As New ListViewItem( New String() _
            {"Programming Windows with C#", _
            "Petzold, Charles", _
            "2001"} )
        Dim item3 As New ListViewItem( New String() _
            {"Coding Techniques for Microsoft Visual Basic .NET", _
            "Connell, John", _
            "2001"} )
        Dim item4 As New ListViewItem( New String() _
            {"C# for Java Developers", _
            "Jones, Allen / Freeman, Adam", _
            "2002"} )
        Dim item5 As New ListViewItem( New String() _
            {"Microsoft .NET XML Web Services Step by Step", _
            "Jones, Allen / Freeman, Adam", _
            "2002"} )
        myListView.Items.AddRange( _
            New ListViewItem() {item0, item1, item2, item3, item4, item5})
        
        If isRunningXPOrLater
            ' Create the groupsTable array and populate it with one 
            ' hash table for each column.
            groupTables = New Hashtable(myListView.Columns.Count) {}
            Dim column As Integer
            For column = 0 To myListView.Columns.Count - 1
                ' Create a hash table containing all the groups 
                ' needed for a single column.
                groupTables(column) = CreateGroupsTable(column)
            Next column
            
            ' Start with the groups created for the Title column.
            SetGroups(0)
        End If
        
        ' Initialize the form.
        Me.Controls.Add(myListView)
        Me.Size = New System.Drawing.Size(550, 330)
        Me.Text = "ListView Groups Example"
    End Sub
    
    <STAThread()> _
    Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New ListViewGroupsExample())
    End Sub
    
    ' Groups the items using the groups created for the clicked 
    ' column.
    Private Sub myListView_ColumnClick( _
        sender As Object, e As ColumnClickEventArgs)

        ' Set the sort order to ascending when changing
        ' column groups; otherwise, reverse the sort order.
        If myListView.Sorting = SortOrder.Descending OrElse _
            isRunningXPOrLater And e.Column <> groupColumn Then
            myListView.Sorting = SortOrder.Ascending
        Else
            myListView.Sorting = SortOrder.Descending
        End If
        groupColumn = e.Column
        
        ' Set the groups to those created for the clicked column.
        If isRunningXPOrLater Then
            SetGroups(e.Column)
        End If
    End Sub
    
    ' 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

    ' Creates a Hashtable object with one entry for each unique
    ' subitem value (or initial letter for the parent item)
    ' in the specified column.
    Private Function CreateGroupsTable(column As Integer) As Hashtable
        ' Create a Hashtable object.
        Dim groups As New Hashtable()
        
        ' Iterate through the items in myListView.
        Dim item As ListViewItem
        For Each item In myListView.Items
            ' Retrieve the text value for the column.
            Dim subItemText As String = item.SubItems(column).Text
            
            ' Use the initial letter instead if it is the first column.
            If column = 0 Then
                subItemText = subItemText.Substring(0, 1)
            End If 

            ' If the groups table does not already contain a group
            ' for the subItemText value, add a new group using the 
            ' subItemText value for the group header and Hashtable key.
            If Not groups.Contains(subItemText) Then
                groups.Add( subItemText, New ListViewGroup(subItemText, _
                    HorizontalAlignment.Left) )
            End If
        Next item
        
        ' Return the Hashtable object.
        Return groups
    End Function 'CreateGroupsTable

    ' Sorts ListViewGroup objects by header value.
    Private Class ListViewGroupSorter
        Implements IComparer 
        
        Private order As SortOrder
        
        ' Stores the sort order.
        Public Sub New(theOrder As SortOrder)
            order = theOrder
        End Sub
        
        ' Compares the groups by header value, using the saved sort
        ' order to return the correct value.
        Public Function Compare(x As Object, y As Object) As Integer _
            Implements IComparer.Compare
            Dim result As Integer = String.Compare( _
                CType(x, ListViewGroup).Header, _
                CType(y, ListViewGroup).Header )
            If order = SortOrder.Ascending Then
                Return result
            Else
                Return -result
            End If
        End Function 'Compare
    End Class 'ListViewGroupSorter 

End Class 'ListViewGroupsExample

注釈

コントロールにListViewGroupCollection ListView.Groups 関連付けられているを取得するには、プロパティを使用しますListViewUse the ListView.Groups property to get the ListViewGroupCollection associated with a ListView control. このコレクションにはListViewGroupListView.Viewプロパティが以外View.Listの値に設定されている場合に、コントロールに表示されるグループを表すオブジェクトが含まれます。This collection contains the ListViewGroup objects that represent the groups shown in the control when the ListView.View property is set to a value other than View.List. グループに割り当てられていないアイテムは、"DefaultGroup{0}" というヘッダーを持つ既定のグループに表示されます。Any items that are not assigned to a group will appear in the default group, which has the header label "DefaultGroup{0}". 既定のグループは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. ListView.Groupsコレクション内にグループが存在しない場合、グループ化機能は無効になります。If there are no groups in the ListView.Groups collection, the grouping feature is disabled.

1つAddのグループをコレクションに追加するには、メソッドを使用します。Use the Add method to add a single group to the collection. コレクション内Insertの特定のインデックスにグループを追加するには、メソッドを使用します。Use the Insert method to add a group at a particular index within the collection. グループを削除するには、 Removeメソッドを使用します。To remove a group, use the Remove method. 特定のRemoveAtインデックス位置にあるグループを削除するには、メソッドを使用します。Use the RemoveAt method to remove the group at a particular index.

ListViewGroupコレクションに複数回追加することはできません。You cannot add a ListViewGroup to the collection more than once. コレクション内のグループの位置を変更するには、まずコレクションから削除してから、目的の場所に挿入する必要があります。To reposition a group within the collection, it must first be removed from the collection, and then inserted at the desired location. Containsメソッドを使用して、特定のグループがコレクション内に既に存在するかどうかを確認します。Use the Contains method to determine whether a particular group is already in the collection. コレクション内のグループのインデックスを取得するには、 IndexOfメソッドを使用します。To retrieve the index of a group within the collection, use the IndexOf method. Item[Int32]インデクサーを使用して、特定のインデックスのグループを取得または設定できます。You can get or set the group at a particular index with the Item[Int32] indexer.

複数のAddRangeグループをコレクションに追加するには、メソッドを使用します。Use the AddRange method to add multiple groups to the collection. グループの配列として、または別ListViewGroupCollection ListViewのコントロールのプロパティをListView.Groups通じて取得するとして、複数のグループを追加できます。You can add multiple groups either as an array of groups or as a ListViewGroupCollection that you retrieve through the ListView.Groups property of another ListView control. コレクションからすべてのグループを削除するには、メソッドを使用します。ClearUse the Clear method to remove all the groups from the collection.

注意

RemoveRemoveAtおよびListViewの各メソッドは、コレクションからグループを削除しますが、コントロールから項目を削除しません。 ClearThe Remove, RemoveAt, and Clear methods remove groups from the collection, but do not remove any items from the ListView control. ListView.Groupsコレクション内にグループが存在しない場合、グループ化機能は無効になり、コントロール内のすべての項目が正常に表示されます。If there are no groups in the ListView.Groups collection, the grouping feature is disabled and all items in the control are displayed normally.

メソッドAddRangeClearメソッドは、 ListViewコントロール内の項目をグループ化する複数の方法を提供する場合に便利です。The AddRange and Clear methods are useful when you want to provide multiple ways to group the items in a ListView control. これを行うには、複数のグループ配列を作成します。To do this, create multiple group arrays. グループ化を変更するには、 Clear最初にメソッドを使用してコレクションからすべてのグループをAddRange削除してから、メソッドを使用して、表示するグループの次の配列を追加します。To change the grouping, first use the Clear method to remove all the groups from the collection, then use the AddRange method to add the next array of groups to display.

メソッドをCopyTo使用して、指定したインデックスを開始位置として、互換性のある配列にコレクション内のグループをコピーします。Use the CopyTo method to copy the groups in a collection to a compatible array starting at a specified index. これは、 Array.Sortメソッドを使用してコレクション内のグループを並べ替える場合などに便利です。This is useful, for example, when you want to sort the groups in the collection using the Array.Sort method. これを行うには、グループを互換性のある配列にコピーしてから、配列を並べ替えます。To do this, copy the groups into a compatible array, then sort the array. 次に、 Clearメソッドを使用してコレクションからすべてのグループを削除しますAddRange 。次に、メソッドを使用して、並べ替えられた配列をコレクションに追加します。Next, use the Clear method to remove all the groups from the collection, then use the AddRange method to add the sorted array back to the collection.

コレクション内Countのグループの数を確認するには、プロパティを使用します。Use the Count property to determine how many groups are in the collection. コレクションを反復処理するには、 IEnumerator GetEnumeratorメソッドから返されたを使用します。To iterate through the collection, use the IEnumerator returned from the GetEnumerator method.

注意

グループ化機能は、アプリケーションがメソッドをApplication.EnableVisualStyles呼び出すときに、windows XP と windows Server 2003 ファミリでのみ使用できます。The grouping feature is available only on Windows XP and the Windows Server 2003 family when your application calls the Application.EnableVisualStyles method. 以前のオペレーティングシステムでは、グループに関連するすべてのコードが無視され、グループは表示されません。On earlier operating systems, any code relating to groups will be ignored and the groups will not appear. その結果、グループ化機能に依存するすべてのコードが正しく機能しなくなる可能性があります。As a result, any code that depends on the grouping feature might not work correctly. グループ化機能が使用可能かどうかを判断し、使用できない場合は代替機能を提供するテストを含めることもできます。You might want to include a test that determines whether the grouping feature is available, and provide alternate functionality when it is unavailable. たとえば、グループ別の並べ替えをサポートしていないオペレーティングシステムで実行するときに、代替の並べ替えを行うことができます。For example, you might want to provide alternate sorting when running on operating systems that do not support sorting by group.

挿入マーク機能は、オペレーティングシステムのテーマ機能を提供するのと同じライブラリによって提供されます。The insertion mark feature is provided by the same library that provides the operating system themes feature. このライブラリが使用可能かどうかを確認するFeatureSupport.IsPresent(Object)には、メソッドオーバーロードをOSFeature.Themes呼び出し、値を渡します。To check for the availability of this library, call the FeatureSupport.IsPresent(Object) method overload and pass in the OSFeature.Themes value.

プロパティ

Count Count Count Count

コレクション内のグループの数を取得します。Gets the number of groups in the collection.

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

コレクション内の指定したインデックスにある ListViewGroup を取得または設定します。Gets or sets the ListViewGroup at the specified index within the collection.

Item[String] Item[String] Item[String] Item[String]

指定された ListViewGroup プロパティ値で、Name を取得または設定します。Gets or sets the ListViewGroup with the specified Name property value.

メソッド

Add(ListViewGroup) Add(ListViewGroup) Add(ListViewGroup) Add(ListViewGroup)

指定された ListViewGroup をコレクションに追加します。Adds the specified ListViewGroup to the collection.

Add(String, String) Add(String, String) Add(String, String) Add(String, String)

新しい ListViewGroup をコレクションに追加します。指定された値を使用して、Name プロパティおよび Header プロパティが初期化されます。Adds a new ListViewGroup to the collection using the specified values to initialize the Name and Header properties

AddRange(ListViewGroupCollection) AddRange(ListViewGroupCollection) AddRange(ListViewGroupCollection) AddRange(ListViewGroupCollection)

既存の ListViewGroupCollection 内のグループをコレクションに追加します。Adds the groups in an existing ListViewGroupCollection to the collection.

AddRange(ListViewGroup[]) AddRange(ListViewGroup[]) AddRange(ListViewGroup[]) AddRange(ListViewGroup[])

コレクションにグループの配列を追加します。Adds an array of groups to the collection.

Clear() Clear() Clear() Clear()

コレクションからすべてのグループを削除します。Removes all groups from the collection.

Contains(ListViewGroup) Contains(ListViewGroup) Contains(ListViewGroup) Contains(ListViewGroup)

指定したグループがコレクション内にあるかどうかを判断します。Determines whether the specified group is located in the collection.

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

コピー先配列の指定されたインデックスを開始位置として、コレクション内のグループを互換性がある 1 次元 Array にコピーします。Copies the groups in the collection to a compatible one-dimensional Array, starting at the specified index of the target array.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

コレクションを反復処理するのに使用できる列挙子を返します。Returns an enumerator used to iterate through the collection.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

既定のハッシュ関数として機能します。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(Inherited from Object)
IndexOf(ListViewGroup) IndexOf(ListViewGroup) IndexOf(ListViewGroup) IndexOf(ListViewGroup)

コレクション内の指定した ListViewGroup のインデックスを返します。Returns the index of the specified ListViewGroup within the collection.

Insert(Int32, ListViewGroup) Insert(Int32, ListViewGroup) Insert(Int32, ListViewGroup) Insert(Int32, ListViewGroup)

コレクション内の指定したインデックス位置に、指定した ListViewGroup を挿入します。Inserts the specified ListViewGroup into the collection at the specified index.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(ListViewGroup) Remove(ListViewGroup) Remove(ListViewGroup) Remove(ListViewGroup)

指定された ListViewGroup をコレクションから削除します。Removes the specified ListViewGroup from the collection.

RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32)

コレクション内の指定されたインデックスにある ListViewGroup を削除します。Removes the ListViewGroup at the specified index within the collection.

ToString() ToString() ToString() ToString()

現在のオブジェクトを表す文字列を返します。Returns a string that represents the current object.

(Inherited from Object)

明示的なインターフェイスの実装

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

コレクションへのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。Gets a value indicating whether access to the collection is synchronized (thread safe).

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

コレクションへのアクセスを同期するために使用できるオブジェクトを取得します。Gets an object that can be used to synchronize access to the collection.

IList.Add(Object) IList.Add(Object) IList.Add(Object) IList.Add(Object)

新しい ListViewGroupListViewGroupCollection に追加します。Adds a new ListViewGroup to the ListViewGroupCollection.

IList.Contains(Object) IList.Contains(Object) IList.Contains(Object) IList.Contains(Object)

指定した値がコレクション内にあるかどうかを判断します。Determines whether the specified value is located in the collection.

IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object)

指定した値のコレクション内のインデックスを返します。Returns the index within the collection of the specified value.

IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object)

ListViewGroupListViewGroupCollection に挿入します。Inserts a ListViewGroup into the ListViewGroupCollection.

IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize

コレクションが固定サイズかどうかを示す値を取得します。Gets a value indicating whether the collection has a fixed size.

IList.IsReadOnly IList.IsReadOnly IList.IsReadOnly IList.IsReadOnly

コレクションが読み取り専用かどうかを示す値を取得します。Gets a value indicating whether the collection is read-only.

IList.Item[Int32] IList.Item[Int32] IList.Item[Int32] IList.Item[Int32]

コレクション内の指定したインデックスにある ListViewGroup を取得または設定します。Gets or sets the ListViewGroup at the specified index within the collection.

IList.Remove(Object) IList.Remove(Object) IList.Remove(Object) IList.Remove(Object)

ListViewGroupListViewGroupCollection から削除します。Removes the ListViewGroup from the ListViewGroupCollection.

拡張メソッド

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable の要素を、指定した型にキャストします。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

指定された型に基づいて IEnumerable の要素をフィルター処理します。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

クエリの並列化を有効にします。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。Converts an IEnumerable to an IQueryable.

適用対象

こちらもご覧ください