Bali C
Bali C

Reputation: 31221

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);

Upvotes: 0

Views: 6401

Answers (2)

Hans Passant
Hans Passant

Reputation: 941277

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);

Upvotes: 1

Samich
Samich

Reputation: 30095

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.

enter image description here

Upvotes: 2

Related Questions