Dev Center > Windows Forms Forums > Windows Forms General > copy listview item to another listview.

Answered copy listview item to another listview.

  • Monday, September 17, 2007 3:10 PM
     
     

    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

Answers

  • Monday, September 17, 2007 4:43 PM
     
     Answered

    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.

     

     

  • Wednesday, September 19, 2007 6:24 AM
     
     Answered

     

    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.

All Replies

  • Monday, September 17, 2007 3:17 PM
     
     

     

    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?

  • Monday, September 17, 2007 4:30 PM
     
     

     

    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);

    }

  • Monday, September 17, 2007 4:40 PM
     
     

    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!!

     

  • Monday, September 17, 2007 4:43 PM
     
     Answered

    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.

     

     

  • Wednesday, September 19, 2007 6:24 AM
     
     Answered

     

    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.