简体   繁体   中英

C# ListView Sub Items

I am trying to add items to a listView that has 2 columns. When I use the code below it will add an item to the first column and nothing to the second then it will create a new line with nothing in the first one and an item in the second, how do I get them both on the same line? Thanks.

listView1.Items.Add(item1);
ListViewItem date = new ListViewItem();
date.SubItems.Add(subitem1);
listView1.Items.Add(date);

This works:

ListViewItem item = new ListViewItem("some item");
item.SubItems.Add(new ListViewItem.ListViewSubItem(item, "sub item"));

this.listView1.Items.Add(item);

Set View property to the Details to check that everything right.

在此处输入图片说明

That's because you are adding two ListViewItems to the ListView. Make it look similar to this:

        var item = new ListViewItem("first");
        item.SubItems.Add("second");
        item.SubItems.Add("third");
        listView1.Items.Add(item);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM