By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,883 Members | 955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,883 IT Pros & Developers. It's quick & easy.

Adding a listview item

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"dotnetnewbie" <do**********@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...
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

Nov 21 '05 #2

P: n/a
thanks

the line

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

was just what I was after.

Gerry

"Stefan De Schepper" wrote:
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

"dotnetnewbie" <do**********@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...
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


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.