Adding a listview item

G

Guest

Whilst looping through items in a listview I wish to have the option of
inserting a new row (in the middle not necessarily at the end of the
listview).

thus if lvw is the listview name

dim itm as listviewitem

For each itm in lvw.Items
'if certain condition is met then insert new row in the listview here
'row must be inserted immediately under the current item
next itm

Thanks
Gerry
 
S

Stefan De Schepper

This does the trick:

Dim itm As listviewitem
Dim MaxItems As Integer = lvw.Items.Count - 1 '0-based
Dim Counter As Integer
Do While Counter <= MaxItems
itm = lvw.Items(Counter)
If CInt(itm.Text.Substring(4, 2)) / 2 = CInt(CInt(itm.Text.Substring(4,
2)) / 2) Then 'don't mind my "certain condition
'if certain condition is met then insert new row in the listview
here
'row must be inserted immediately under the current item
lvw.Items.Insert(itm.Index, New ListViewItem("New item"))
Counter += 1
MaxItems += 1
End If
Counter += 1
Loop

Hope this helps,
Stefan
 
G

Guest

thanks

the line

lvw.Items.Insert(itm.Index, New ListViewItem("New item"))

was just what I was after.

Gerry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top