Listview复制多行并将其粘贴为新行 [英] Listview copy multiple lines and paste them back as new rows
问题描述
我使用此代码:
I use this code:
ListViewItem clone;
int hasclone = 0;
复制:
Copy:
foreach (ListViewItem item in listView1.SelectedItems)
{
clone = (ListViewItem)item.Clone();
hasclone = 1;
}
粘贴:
Paste:
if (hasclone == 1)
{
hasclone = 0;
nummer = nummer + 1;
listView1.Items.Insert(actions, clone);
actions = actions + 1;
sortlist();
}
我可以像这样复制并粘贴列表视图中的一行,
但是
它只贴一个(或复制一个)
可以有人帮我这个吗?
例如:
Listview
Item1 Morestuff Morestuff
Item2 Morestuff Morestuff
当我复制Item1和Item2
它将Item2粘贴为Item3
所以基本上它只复制最后一个因为
I can copy and paste a row in the listview like this,
BUT
It only pastes one (or copies one)
Can someone help me with this?
For example:
Listview
Item1 Morestuff Morestuff
Item2 Morestuff Morestuff
When i copy Item1 and Item2
It pastes Item2 as Item3
So basicly it only copys the last one because of
foreach (ListViewItem item in listView1.SelectedItems)
{
clone = (ListViewItem)item.Clone();
hasclone = 1;
}
我理解所以它只能复制1,所以只粘贴1个listview项目
我希望能够复制和粘贴多个项目
Wich i understand so it can only copy 1 and so only paste 1 listview item
I would like to be able to copy and paste multiple items
推荐答案
尝试类似:
更改存储副本的方式:
Try something like:
Change how you store the copy:
List<ListViewItem> clones = new List<ListViewItem>();
变量 hasclone
是不必要的。
然后将它们全部复制:
Variable hasclone
is unnecessary.
Then copy them all:
clones.Clear();
foreach (ListViewItem item in listView1.SelectedItems)
{
clones.Add((ListViewItem)item.Clone());
}
并将它们全部粘贴:
And paste them all:
if (clones.Any()) //.Any() is from System.Linq
{
++nummer; // is this incremented per paste, or per pasted value?
// if per value, move it inside the foreach below
foreach (ListViewItem clone in clones)
{
listView1.Items.Insert(actions++, clone);
}
sortlist();
}
祝你好运!
这篇关于Listview复制多行并将其粘贴为新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!