The System.Windows.Forms.ListViewGroup class in C# is used to group items in a ListView control. By using this class, you can organize the items and make them easy to navigate.
Example 1: Grouping Items by First Letter
In this example, we are grouping the items in a ListView control by their first letter. First, we create a new ListViewGroup object for each letter of the alphabet. Then we add the items to the corresponding group based on their first letter.
ListViewGroup[] groups = new ListViewGroup[26];
for (int i = 0; i < 26; i++) { groups[i] = new ListViewGroup(((char)('A' + i)).ToString()); listView1.Groups.Add(groups[i]); }
foreach (ListViewItem item in listView1.Items) { groups[item.Text[0] - 'A'].Items.Add(item); }
This code example is using the System.Windows.Forms package library.
Example 2: Grouping Items by Category
In this example, we are grouping the items in a ListView control by their category. We first create a ListViewGroup object for each category. Then we add the items to the appropriate group based on their category.
ListViewGroup group1 = new ListViewGroup("Category 1"); ListViewGroup group2 = new ListViewGroup("Category 2");
foreach (ListViewItem item in listView1.Items) { if (item.SubItems[1].Text == "Category 1") group1.Items.Add(item); else group2.Items.Add(item); }
This code example is also using the System.Windows.Forms package library.
C# (CSharp) System.Windows.Forms ListViewGroup - 60 examples found. These are the top rated real world C# (CSharp) examples of System.Windows.Forms.ListViewGroup extracted from open source projects. You can rate examples to help us improve the quality of examples.