本文介绍了C#将项目从listview1复制/移动到带数量的listview2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将双击项从 listview1 复制到 listview2 ,到目前为止,我已经在 listview1 上使用了此代码鼠标双击事件.

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

当我双击该项目时,将有关所选项目的所有内容复制到我的 listview2 中,无论如何,这并不是我真正想要的.让我在 listview1 中说我得到了这个物品:

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

我想要的是每当我双击 listview1 上的项目时,数量应减少1,因此在listview1上将如下所示:

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

然后将选中的商品添加到 listview2 中,数量为1,如下所示:

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

ID   |  ITEMNAME | QUANTITY
1    |  ITEM1    | 1

再次双击同一项目后,它在 listview1 上执行相同的操作,但是我不希望它重复 listview2 上的项目.只需将数量+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?

推荐答案

有两种不同的方式.

如果要将项目从listview1复制到listview2:

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

如果要将项目从listview1移到listview2:

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

这篇关于C#将项目从listview1复制/移动到带数量的listview2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:46