copy the items of a listview to the other

georgepatotk

Contributor
Joined
Mar 1, 2004
Messages
432
Location
Malaysia
I want to copy the whole list of a listview to another without using iteration statement because it could be time consuming if my data volume is large.

I had tried lv1 = lv2, this doesn't seems to work correctly because lv2 is created in other class.

I wish to do something like
lv1.Items = lv2.items

Please give me a clue to do this with iteration control. Thanks in advance..
 
The Items property is readonly, so of course you can't do lv2.Items=lv1.Items. You can't do lv1 = lv2 because then your just setting your variable, lv2, to point to a whole different listview: lv1, not a different set of listview items.

This is a way you could copy list view items from one listview to another...

Visual Basic:
1  'Assuming you have two listviews, named lv1 and lv2...
2  Dim MyItems As ListViewItem() = New ListViewItem(lv1.Items.Count-1)
3  lv1.Items.CopyTo(MyItems)
4  lv1.Items.Clear 'You CANT have a listview item in more than one listview at a time
5  lv2.Items.AddRange(MyItems)

Notice line 4. If you want to have the items appear in both listviews simultaneously, as far as I can see, you must iterate through them and clone each one. And if you don't need them in both listviews at once (if, for instance, the listviews are on separate forms and only one is displayed at a time) you could conceivably remove the listview from one form and add it to the other. Another thing you could do is just populate both listviews at the same time.

If you can't do any of those, you can always use for loop and slap up a progress bar.
 
Back
Top