Home >Backend Development >C++ >How to Add Items to Multiple Columns in a ListView Control?

How to Add Items to Multiple Columns in a ListView Control?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-31 09:08:09227browse

How to Add Items to Multiple Columns in a ListView Control?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn