Howto add items to ListView at runtime?

jwilkins

Member
Joined
Jan 15, 2008
Messages
5
Programming Experience
1-3
Hi ive got a listview control on my form and it has a generic list of items added to it.
:confused:What ive tried to do and can't seem to figure out is how can i add items to this list (preferably to specific groups) during runtime. The reason for doing it at runtime is that the specific items to add are based and can change relating to a set of variables.
Please Help, thanks.
 
listview.items.add() method
 
oh you know ive tried out so many different lines of code and erased them to try something else i dont even know what i would paste in here for you to look at. Maybe if youve got the time just kinda point me in the right direction. do i need to refresh the listview after adding items or something. ive tried refreshing and havent seen a change but maybe i didnt do it right.
i can add in items at run time and they show up but when i try to put them into a group (that is already created before runtime) at a specified index they wont show up. is there some kindof stepped process i may not be following like create a new group at runtime then add an item to the group or something. if this doesnt help ill get what code i have up here but like i said ive tried so many approaches i dont know if what i put up will be helpful but if thats what will help most, so be it.
 
The group have to be set on the item itself, either with one of the constructor overloads or with the Group property. Example of both:
VB.NET:
Expand Collapse Copy
Dim item As ListViewItem

item = Me.ListView1.Items.Add("item1")
item.Group = Me.ListView1.Groups("ListViewGroup1")

item = New ListViewItem("item2", Me.ListView1.Groups("ListViewGroup2"))
Me.ListView1.Items.Add(item)
If you create items in Designer you can just select the Group from available list.
 
yep it worked alright, like a champ! thanks, by the way i didnt need the last line of code i assume you put it in just as another example of how to do it.
 
ok so my next question is now im trying to add a sub item to the item ive just added to the list view and i cant get it to show up.
Heres my code, when you get time maybe you could help me out.
Thanks

VB.NET:
Expand Collapse Copy
Dim blSubItem As ListViewItem.ListViewSubItem
Dim advLev As String
advLev = Cstr(VB_CharGen.Form1.RAD.AMB)
blSubItem = New ListViewItem.ListViewSubItem(biglist, advLev)

biglist.SubItems.Add(advLev)
biglist.SubItems.Item(1).Text = advLev
 
Last edited:
continuing from the sample above:
VB.NET:
Expand Collapse Copy
item.SubItems.Add("sub1")
 
Back
Top