none
How to copy all items in One listview into another one listview.

    Question

  • HI,

    I like to copy all items in One listview into another one listview.

    I tried to do like this:

    1way:

    foreach (ListViewItem lvi in listview1.Items)

    {

    this.listview2.Items.Add(lvi);

    }



    But it shows Argument Exception: Value does not fall within the expected range

    I tried in another way also :

    2nd way:

    for(int i = 0; i < listview1.Items.Count; i++)

    {

    this.listview2.Items.Add(listview1.Items[i]);

    }

    Then also i am getting the same argument exception..

    Any one could u pls help to solve this problem..and copy all items in One listview into another one listview.

    Thanks in advance

    Regards,

    M.Sathya.

    Friday, March 20, 2009 8:37 AM

Answers

  • Hi,

    I was getting the same exception you were getting however the following code seems to work for me :-

                List<ListViewItem> myList = new List<ListViewItem>();  
     
                foreach (ListViewItem lvi in this.listView1.Items)  
                {  
                    myList.Add(lvi);                  
                }  
     
                this.listView1.Items.Clear();  
                  
                foreach (ListViewItem lvi in myList)  
                {  
                    this.listView2.Items.Add(lvi);  
                } 

    It seems that the first ListView needs to have its items cleared before you can add the items to another list view, therefore you have to copy the items to a temporary location before clearing.
    I am not too sure why this is happening, however the above should be a working solution.

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    Saturday, March 21, 2009 10:41 AM

All replies

  • Hi,

    I was getting the same exception you were getting however the following code seems to work for me :-

                List<ListViewItem> myList = new List<ListViewItem>();  
     
                foreach (ListViewItem lvi in this.listView1.Items)  
                {  
                    myList.Add(lvi);                  
                }  
     
                this.listView1.Items.Clear();  
                  
                foreach (ListViewItem lvi in myList)  
                {  
                    this.listView2.Items.Add(lvi);  
                } 

    It seems that the first ListView needs to have its items cleared before you can add the items to another list view, therefore you have to copy the items to a temporary location before clearing.
    I am not too sure why this is happening, however the above should be a working solution.

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    Saturday, March 21, 2009 10:41 AM
  • HI Paul,

           I tried ur solution....It is working fine .. :)..Greate.
    Thank you very much for ur kind reply

    Regards,
    M.Sathya.

    Monday, March 23, 2009 5:47 AM
  • Another way to skin the same cat:

    for (int nIdx = 0; nIdx < listViewOrig.Items.Count; nIdx++)
    {
    ListViewItem lvi = (ListViewItem) listViewOrig.Items[nIdx].Clone();
    listViewNew.Items.Add(lvi);
    }


    If nothing else, it's a good intro into using the "Clone" function.


    Bob
    • Proposed as answer by BobDY Tuesday, May 05, 2009 6:21 PM
    Tuesday, May 05, 2009 6:21 PM
  • Hello,

           Can I use the same code for Excel VBA Excel 2007

    Saturday, July 26, 2014 11:21 AM