Reputation: 259
i have ListView with items one ListView with items another one is empty i need to copy from first ListView selected item to another ListView at the same time i have to remove the selected item in first ListView in C#
Upvotes: 0
Views: 552
Reputation: 45809
Rather than removing items from the collection that you're enumerating (as per Wael's answer), which is a "bad idea", use a temporary collection, in this instance a List to store them in before removing them:
List<ListViewItem> itemsToMove = new List<ListViewItem>();
foreach (ListViewItem item in listView1.SelectedItems)
{
itemsToMove.Add(item);
}
foreach (ListViewItem item in itemsToMove)
{
listView1.Items.Remove(item);
listView2.Items.Add(item);
}
Where listView1 is the list with the selected items and listView2 is the list to move them to.
Upvotes: 3
Reputation: 23044
this code will copy from the first listview1 to listview2 all the selected items and delete it from listview1
foreach (ListViewItem itm in ListView1.SelectedItems)
{
ListView1.Items.Remove(itm);
listView2.Items.Add(itm);
}
Upvotes: 0