Add to ListView

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I am Attempting to add data to a listview control......I know how to add the data if I have all the datta at the same time...but I need to add data at different times......
so this works if I know the data ....

ListViewitem lvi = new ListViewItem(data);
lvi.Subitems.Add(data);
lvi.SubItems.Add(data);
listView1.Items.Add(lvi);


how do I add data to the same listView at different times and to different subitems.....if that makes any sence...

thank you

-InkedGFX
 
You do it exactly the same way. You create the ListViewItem and add it to ListView whenever you're ready. When you want to add subitems then you get a reference to the item and add top its SubItems collection, just as you've done there. The only different is how you get the item reference but surely you already know how to get an item from a ListView.
 
well...to be honest , I'm not sure....I tried a few different things to add the newer items to the listview...I thought this would work.

lvi.SubItems[2].Add(data)


doesn't work.....I thought the [2] would put the data in the correct subitem column.....no good!

-InkedGFX
 
OK, you're not listening to what I'm saying. I said that you do it EXACTLY the same way. Did you use [2] in the code you posted originally? If not then you obviously don't do it here either because then it wouldn't be exactly the same. You can't get the subitem at index 2 if you haven't added at least three subitems in the first place, which you haven't. If you could get that subitem, would it have an Add method anyway?

It's important to understand that a ListView is NOT a grid control. The number of column headers in the ListView has absolutely no relation to the number of subitems in each item. Remember that Details is just one of several views a ListView can display. What if the View was LargeIcons? What of your columns then? There are none, yet the items can still have subitems. Even in Details view, the number of columns determines the maximum number of subitems that can be displayed for each item, but each item can have more or fewer than that.

When you create an item, it initially has no subitems at all (other than the default one at index zero) so if you want an item to have subitems then you have to add them. You do that EXACTLY as you did it in your original code.
 
Back
Top Bottom