Adding Items to Multiple Columns in a ListView Control
Adding items to the first column of a ListView control is straightforward using listView1.Items.Add. However, to add items to subsequent columns, a slightly different approach is required.
Solution 1
Using the SubItems property of the ListViewItem, you can add items to specific columns. For instance, to add items to the first four columns:
string[] row1 = { "s1", "s2", "s3" }; listView1.Items.Add("Column1Text").SubItems.AddRange(row1);
Solution 2
Alternatively, you can create individual ListViewItem objects for each row and add subitems using SubItems.Add:
ListViewItem item1 = new ListViewItem("Something"); item1.SubItems.Add("SubItem1a"); item1.SubItems.Add("SubItem1b"); item1.SubItems.Add("SubItem1c"); ListViewItem item2 = new ListViewItem("Something2"); item2.SubItems.Add("SubItem2a"); item2.SubItems.Add("SubItem2b"); item2.SubItems.Add("SubItem2c"); ListViewItem item3 = new ListViewItem("Something3"); item3.SubItems.Add("SubItem3a"); item3.SubItems.Add("SubItem3b"); item3.SubItems.Add("SubItem3c"); ListView1.Items.AddRange(new ListViewItem[] {item1,item2,item3});
These solutions provide flexibility in adding items to multiple columns in a ListView control, allowing for dynamic data population.
The above is the detailed content of How to Add Items to Multiple Columns in a ListView Control?. For more information, please follow other related articles on the PHP Chinese website!