Home >Backend Development >C++ >How to Add Items to Specific Columns in a C# ListView?
Adding Items to Specific Columns in C# ListView
In a ListView control within a Windows form, the default method for adding items utilizes the listView1.Items.Add function, successfully targeting column 1. This article delves into techniques for populating data into subsequent columns (2, 3, etc.).
Solution 1: Using SubItems.AddRange
This approach enables adding multiple items to columns 2 and beyond in a single line:
string[] row1 = { "s1", "s2", "s3" }; listView1.Items.Add("Column1Text").SubItems.AddRange(row1);
Solution 2: Constructing ListViewItems
A more verbose alternative involves creating ListViewItem objects and explicitly defining their subitems:
ListViewItem item1 = new ListViewItem("Something"); item1.SubItems.Add("SubItem1a"); item1.SubItems.Add("SubItem1b"); item1.SubItems.Add("SubItem1c");
Repeating this pattern for each row and then adding the items to the ListView completes the population process.
The above is the detailed content of How to Add Items to Specific Columns in a C# ListView?. For more information, please follow other related articles on the PHP Chinese website!