C# Copy/Move item from listview1 to listview2 with quantity

好久不见. 提交于 2019-12-11 14:23:56

问题


I would like to know how do I copy the double-click item from listview1 to listview2, so far I had using this code on listview1 mouse double click event.

foreach (ListViewItem item in lvItemlist.SelectedItems)
        {
            lvItemBuy.Items.Add((ListViewItem)item.Clone());
        }

When I double click on the item its copy everything about the selected item to my listview2, anyway this is not really what i want..lets say in my listview1 I got this item:

ID   |  ITEMNAME | QUANTITY
1    |  ITEM1    | 100

What I want is everytime I double-click on the item on listview1, the quantity should decrease by 1, so it will be like this on listview1:

ID   |  ITEMNAME | QUANTITY
1    |  ITEM1    | 99

then added the selected item to listview2 with 1 quantity like this:

ID   |  ITEMNAME | QUANTITY
1    |  ITEM1    | 1

After double click again on the same item, it do the same thing on listview1 but i dont want it to duplicate the item on listview2. Simply just +1 the quantity. Is there a way to do this?


回答1:


There are many ways to do this. You can introduce Id's for the listviewItems. Don't close the ListViewItem direct, instead write a method which copies the properties from the first listviewitem to the second one. In this method you can decrease the quantity and check if the selected listviewitem is already in the second listview




回答2:


There are two different different ways.

If you want to copy the items from listview1 to listview2:

private static void CopySelectedItems(ListView source, ListView target)
{
    foreach (ListViewItem item in source.SelectedItems)
    {
        target.Items.Add((ListViewItem)item.Clone());
    }
}

If you want to move the items from listview1 to listview2:

private static void MoveSelectedItems(ListView source, ListView target)
{    
    while (source.SelectedItems.Count > 0)
    {
        ListViewItem temp = source.SelectedItems[0];
        source.Items.Remove(temp);
        target.Items.Add(temp);
    }            
}


来源:https://stackoverflow.com/questions/15378486/c-sharp-copy-move-item-from-listview1-to-listview2-with-quantity

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!