Andr&#233&#59; Dani

Hi, i would like to copy items from listview2 to listview3

my code look like this

foreach (ListViewItem item in istview2.item)

{

listview3.items.add(item);

}

but it wont work!

tnx




Re: Windows Forms General copy listview item to another listview.

H. Tony

if it's

Code Snippet

foreach (ListViewItem item in listview2.Items)

when ever this code runs, it should add items from listview2 to listview3.

what do you mean by 'it wont work'

nothing added have you debugged into the code to see if the code is executed, if listview2 contains item

or






Re: Windows Forms General copy listview item to another listview.

Eli Gazit

You can't add a ListViewItem to a ListView with it is already a part of another ListView.

You need to Remove the item from the source list before sending it to the target list.

foreach (ListViewItem item in listView1.Items)

{

listView1.Items.Remove(item);

listView2.Items.Add(item);

}






Re: Windows Forms General copy listview item to another listview.

André Dani

way do i have to remove the item from listview1

i just want to copy it..

do u have another suggestion

or do u have a better solution

//thanks best reggards!!






Re: Windows Forms General copy listview item to another listview.

Eli Gazit

If you want to copy the item, rather than removing it, create a new instanse of the ListViewItem, set its properties to the source item and add it to the target list.






Re: Windows Forms General copy listview item to another listview.

Rong-Chun Zhang - MSFT

Hi Dani,

Yes, like Gazit said, you need a new instance of ListViewItem. And the ListViewItem has a method named ‘Clone’ which allows you to create an identical copy of the item. Try something like the following:

Code Snippet

foreach (ListViewItem item in this.listView2.Items)

{

this.listView3.Items.Add((ListViewItem)item.Clone());

}

I assume your listView2 and listView3 have the same columns.

Best regards.
Rong-Chun Zhang.