tags:

views:

188

answers:

3

Hello there,

Whats the best way to swap two ListView items in C#? I see that the standard ListView doesn't implement such functionality.

-- Best Regards, Murat

A: 

Both ASP.NET and Winforms ListView have Items property which allows to add or removed items.

Andrew Bezzub
A: 

I found this post: http://www.knowdotnet.com/articles/listviewmoveitem.html

Thanks.

Murat
+1  A: 

I´ve wrote a little sample that should work.

    ListViewItem[] copyOfItemsInListView1 = new ListViewItem[listView1.Items.Count];
    ListViewItem[] copyOfItemsInListView2 = new ListViewItem[listView2.Items.Count];

    listView1.Items.CopyTo(copyOfItemsInListView1, 0);
    listView2.Items.CopyTo(copyOfItemsInListView2, 0);

    listView1.Items.Clear();
    listView2.Items.Clear();

    for (int i = 0; i < copyOfItemsInListView2.Length; i++)
    {
        listView1.Items.Add(copyOfItemsInListView2[i]);
    }
    for (int i = 0; i < copyOfItemsInListView1.Length; i++)
    {
        listView2.Items.Add(copyOfItemsInListView1[i]);
    }
Javier Morillo