ListViewGroup クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


ListView のグループ化機能を利用して、論理的に関連する ListView 項目のビジュアル グループを作成できます。各グループは、テキスト ベースのヘッダー、水平線、そのグループに割り当てられた項目で構成されます。ヘッダー テキストは、コントロールの左側、右側、または中央に揃えて配置できます。ListView コントロールに割り当てられたグループは、ListView.View プロパティが View.List 以外の値に設定されているときに常に表示されます。
ListView グループを利用して項目をカテゴリに分類することで、項目を探すときに見つけやすくなります。必要なカテゴリを任意に作成できます。項目をグループ化する一般的な手法の 1 つは、並べ替えの方法によるグループ化です。たとえば、項目がアルファベット順に並んでいる場合、項目の最初の文字を使ってグループ化できます。また、詳細ビューの列ヘッダーをクリックして項目を並べ替えれば、種類や日付などのサブ項目でグループ化できます。Windows エクスプローラでは、この種のグループ化が使用されます。
グループ化機能を使用するには、ListView コントロールの ListView.Groups コレクションに 1 つ以上の ListViewGroup オブジェクトを追加します。ListViewGroup コンストラクタ、または Header プロパティと HeaderAlignment プロパティを使用して、グループ ヘッダー テキストとヘッダー配置を設定します。
グループに項目を割り当てるには、ListViewItem コンストラクタでグループを指定するか、ListViewItem.Group プロパティを設定するか、グループの Items コレクションに項目を直接追加します。項目が表示される前に、すべての項目をグループに割り当てる必要があります。グループに割り当てられていない項目は、ヘッダー ラベル "DefaultGroup{0}" を持つ既定のグループに表示されます。既定のグループは ListView.Groups コレクションに格納されていないため、変更できません。特に、デバッグですべての項目がグループに適切に追加されていることを確認する際に有効です。
1 つの項目は、同時に 1 つのグループにしか属すことができません。項目が属しているグループを変更するには、実行時に ListViewItem.Group プロパティを設定するか、項目を別のグループの Items コレクションに追加します。この項目は、以前に属していたグループから自動的に削除されます。
![]() |
---|
グループを使用する際には、挿入マーク機能は無効になります。グループ化機能では項目をグループのメンバシップによって整理しますが、挿入マーク機能では、項目が整理されていない ListView コントロール内でドラッグ アンド ドロップの位置変更を使用するからです。 |
![]() |
---|
ListView グループは、Windows XP および Windows Server 2003 ファミリでアプリケーションから Application.EnableVisualStyles メソッドを呼び出す場合にだけ使用できます。旧バージョンのオペレーティング システムでは、グループに関するコードがすべて無視され、グループが表示されません。その結果、グループ化機能に依存するコードが正常に機能しない可能性があります。グループ化機能が使用できるかどうかを確認し、使用できない場合は代替機能を提供するテスト機能を用意します。たとえば、グループによる並べ替えをサポートしないオペレーティング システムで実行する場合には、別の並べ替え方法を用意します。 挿入マーク機能は、オペレーティング システムのテーマ機能を提供しているライブラリと同じライブラリによって提供されます。このライブラリが使用できるかどうかを確認するには、FeatureSupport.IsPresent(Object) メソッド オーバーロードを呼び出し、OSFeature.Themes 値を渡します。 |

ListView のグループ化機能を使用して、詳細ビューのサブ項目の値に従って項目を整理する方法を次のコード例に示します。この形式のグループ化は、Windows エクスプローラで使用されているグループ化と似ています。この例では、グループが動的に作成されます。各サブ項目列では、一意のサブ項目値ごとに 1 つのグループが作成されます。親項目列では、一意の先頭の文字ごとに 1 つのグループが作成されます。列のヘッダーをクリックすると、その列に対して作成されたグループに項目が並べ替えられます。同じ列ヘッダーを再度クリックすると、グループの順序が反転します。
Imports System 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 'New <STAThread()> _ Shared Sub Main() Application.EnableVisualStyles() Application.Run(New ListViewGroupsExample()) End Sub 'Main ' 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 'myListView_ColumnClick ' 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 'SetGroups ' 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 'New ' 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
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; } } } }
#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()); }
import System.*; import System.Collections.*; import System.Windows.Forms.*; public class ListViewGroupsExample extends Form { private ListView myListView; private boolean isRunningXPOrLater = false; // Declare a Hashtable array in which to store the groups. private Hashtable groupTables[]; // Declare a variable to store the current grouping column. private int groupColumn = 0; public ListViewGroupsExample() { // Initialize myListView. myListView = new ListView(); myListView.set_Dock(DockStyle.Fill); myListView.set_View(View.Details); myListView.set_Sorting(SortOrder.Ascending); // Create and initialize column headers for myListView. ColumnHeader columnHeader0 = new ColumnHeader(); columnHeader0.set_Text("Title"); columnHeader0.set_Width(-1); ColumnHeader columnHeader1 = new ColumnHeader(); columnHeader1.set_Text("Author"); columnHeader1.set_Width(-1); ColumnHeader columnHeader2 = new ColumnHeader(); columnHeader2.set_Text("Year"); columnHeader2.set_Width(-1); // Add the column headers to myListView. myListView.get_Columns().AddRange(new ColumnHeader[] { columnHeader0, columnHeader1, columnHeader2 }); // Add a handler for the ColumnClick event. myListView.add_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.get_Items().AddRange(new ListViewItem[] { item0, item1, item2, item3, item4, item5 }); // Determine whether Windows XP or a later // operating system is present. if (System.Environment.get_OSVersion().get_Version().get_Major() > 5 || (System.Environment.get_OSVersion().get_Version().get_Major() == 5 && System.Environment.get_OSVersion().get_Version().get_Minor() >= 1)) { isRunningXPOrLater = true; } if (isRunningXPOrLater) { // Create the groupsTable array and populate it with one // hash table for each column. groupTables = new Hashtable[myListView.get_Columns().get_Count()]; for (int column = 0; column < myListView.get_Columns().get_Count(); column++) { // Create a hash table containing all the groups // needed for a single column. groupTables.set_Item(column, CreateGroupsTable(column)); } // Start with the groups created for the Title column. SetGroups(0); } // Initialize the form. this.get_Controls().Add(myListView); this.set_Size(new System.Drawing.Size(550, 330)); this.set_Text("ListView Groups Example"); } //ListViewGroupsExample /** @attribute STAThread() */ public static void main(String[] args) { Application.Run(new ListViewGroupsExample()); } //main // 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.get_Sorting().Equals(SortOrder.Descending) || (isRunningXPOrLater && e.get_Column() != groupColumn)) { myListView.set_Sorting(SortOrder.Ascending); } else { myListView.set_Sorting(SortOrder.Descending); } groupColumn = e.get_Column(); // Set the groups to those created for the clicked column. if (isRunningXPOrLater) { SetGroups(e.get_Column()); } } //myListView_ColumnClick // Sets myListView to the groups created for the specified column. private void SetGroups(int column) { // Remove the current groups. myListView.get_Groups().Clear(); // Retrieve the hash table corresponding to the column. Hashtable groups = (Hashtable)groupTables.get_Item(column); // Iterate through the items in myListView, assigning each // one to the appropriate group. for (int iCtr = 0; iCtr < myListView.get_Items().get_Count(); iCtr++) { ListViewItem item = myListView.get_Items().get_Item(iCtr); // Retrieve the subitem text corresponding to the column. String subItemText = item.get_SubItems().get_Item(column).get_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.set_Group((ListViewGroup)(groups.get_Item(subItemText))); } // Copy the groups for the column to an array. ListViewGroup groupsArray[] = new ListViewGroup[groups.get_Count()]; groups.get_Values().CopyTo(groupsArray, 0); // Sort the groups and add them to myListView. Array.Sort(groupsArray, new ListViewGroupSorter( myListView.get_Sorting())); myListView.get_Groups().AddRange(groupsArray); } //SetGroups // 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. for (int iCtr = 0; iCtr < myListView.get_Items().get_Count(); iCtr++) { ListViewItem item = myListView.get_Items().get_Item(iCtr); // Retrieve the text value for the column. String subItemText = item.get_SubItems().get_Item(column).get_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; } //CreateGroupsTable // Sorts ListViewGroup objects by header value. private class ListViewGroupSorter implements IComparer { private SortOrder order; // Stores the sort order. public ListViewGroupSorter(SortOrder theOrder) { order = theOrder; } //ListViewGroupSorter // 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).get_Header(), ((ListViewGroup)y).get_Header()); if (order.Equals(SortOrder.Ascending)) { return result; } else { return -result; } } //Compare } //ListViewGroupSorter } //ListViewGroupsExample

System.Windows.Forms.ListViewGroup


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ListViewGroup コンストラクタ ()
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ListViewGroup コンストラクタ (String, HorizontalAlignment)
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim header As String Dim headerAlignment As HorizontalAlignment Dim instance As New ListViewGroup(header, headerAlignment)

詳細ビューのサブ項目の値を使って ListView 項目を整理するアプリケーションで、ListViewGroup コンストラクタを使用する方法を次のコード例に示します。この形式のグループ化は、Windows エクスプローラで使用されているグループ化と似ています。この例では、グループが動的に作成されます。各サブ項目列では、一意のサブ項目値ごとに 1 つのグループが作成されます。親項目列では、一意の先頭の文字ごとに 1 つのグループが作成されます。各列について作成されたグループは、サブ項目のテキストまたは先頭の文字と共にハッシュ テーブルに格納されます。列ヘッダーがクリックされると、このテキスト値を使用して項目が適合する列のグループを決定します。
コード全体については、ListViewGroup の概要のリファレンス トピックを参照してください。
' 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
// 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; }
// 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; }
// 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. for (int iCtr = 0; iCtr < myListView.get_Items().get_Count(); iCtr++) { ListViewItem item = myListView.get_Items().get_Item(iCtr); // Retrieve the text value for the column. String subItemText = item.get_SubItems().get_Item(column).get_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; } //CreateGroupsTable

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ListViewGroup コンストラクタ (String, String)
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ListViewGroup コンストラクタ (String)
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ListViewGroup コンストラクタ

名前 | 説明 |
---|---|
ListViewGroup () | 既定の左揃えヘッダーで、"ListViewGroup" の既定のヘッダー テキストを使用して ListViewGroup クラスの新しいインスタンスを初期化します。 |
ListViewGroup (String) | Header プロパティの初期値を指定して、既定の左揃えヘッダーで ListViewGroup クラスの新しいインスタンスを初期化します。 |
ListViewGroup (String, HorizontalAlignment) | 指定されたヘッダー配置で、指定されたヘッダー テキストを使用して ListViewGroup クラスの新しいインスタンスを初期化します。 |
ListViewGroup (String, String) | Name プロパティと Header プロパティの初期値を指定して、ListViewGroup クラスの新しいインスタンスを初期化します。 |

ListViewGroup プロパティ

名前 | 説明 | |
---|---|---|
![]() | Header | グループのヘッダー テキストを取得または設定します。 |
![]() | HeaderAlignment | グループ ヘッダー テキストの配置を取得または設定します。 |
![]() | Items | このグループに関連付けられているすべての項目を格納するコレクションを取得します。 |
![]() | ListView | このグループを格納している ListView コントロールを取得します。 |
![]() | Name | グループの名前を取得または設定します。 |
![]() | Tag | グループに関するデータを格納するオブジェクトを取得または設定します。 |

関連項目
ListViewGroup クラスSystem.Windows.Forms 名前空間
ListView クラス
ListViewGroupCollection
ListViewItem.Group
ListView.ListViewItemCollection クラス
ListViewGroup メソッド

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | オーバーライドされます。 |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | System.Runtime.Serialization.ISerializable.GetObjectData | SerializationInfo に、オブジェクトをシリアル化するために必要なデータを設定します。 |

関連項目
ListViewGroup クラスSystem.Windows.Forms 名前空間
ListView クラス
ListViewGroupCollection
ListViewItem.Group
ListView.ListViewItemCollection クラス
ListViewGroup メンバ
ListView コントロール内に表示される項目のグループを表します。
ListViewGroup データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | Header | グループのヘッダー テキストを取得または設定します。 |
![]() | HeaderAlignment | グループ ヘッダー テキストの配置を取得または設定します。 |
![]() | Items | このグループに関連付けられているすべての項目を格納するコレクションを取得します。 |
![]() | ListView | このグループを格納している ListView コントロールを取得します。 |
![]() | Name | グループの名前を取得または設定します。 |
![]() | Tag | グループに関するデータを格納するオブジェクトを取得または設定します。 |

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | オーバーライドされます。 |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | System.Runtime.Serialization.ISerializable.GetObjectData | SerializationInfo に、オブジェクトをシリアル化するために必要なデータを設定します。 |

関連項目
ListViewGroup クラスSystem.Windows.Forms 名前空間
ListView クラス
ListViewGroupCollection
ListViewItem.Group
ListView.ListViewItemCollection クラス
- ListViewGroupのページへのリンク