Adding Items to Multiple Columns in a ListView Control (C#)?
When working with a ListView control in a WinForms application, you may encounter the need to populate multiple columns within each row. This article delves into several methods for achieving this using the 'ListViewItem' class and its 'SubItems' property.
Method 1: Using the 'SubItems.AddRange' Method
For a concise approach, you can utilize the 'SubItems.AddRange' method to add items to multiple columns simultaneously.
string[] row1 = { "s1", "s2", "s3" }; listView1.Items.Add("Column1Text").SubItems.AddRange(row1);
Method 2: Using the 'SubItems.Add' Method
Alternatively, you can use the 'SubItems.Add' method to add items to individual columns more explicitly.
ListViewItem item1 = new ListViewItem("Something"); item1.SubItems.Add("SubItem1a"); item1.SubItems.Add("SubItem1b"); item1.SubItems.Add("SubItem1c");
Method 3: Using an Array of 'ListViewItem' Objects
If you prefer to create 'ListViewItem' objects manually, you can populate them with subitems and then add them to the 'ListView' using 'AddRange'.
ListViewItem[] items = new ListViewItem[] { new ListViewItem("Something1") { SubItems = { "a", "b", "c" } }, new ListViewItem("Something2") { SubItems = { "d", "e", "f" } } }; listView1.Items.AddRange(items);
The above is the detailed content of How to Add Items to Multiple Columns in a C# ListView Control?. For more information, please follow other related articles on the PHP Chinese website!