如何在 c#net 中单击按钮时将所选项目从一个列表视图复制到另一个列表视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2340439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to copy the selected items from one listview to another on button click in c#net?
提问by zoya
How can I copy selected items from one listview to another on button click..?? without any redundancyalso can I give the option for multiple selection of items and adding them in a bulk without using the ctrl from keyboard?? making it user friendly can we use checkboxes and how will they work?? The code below is used to copy the entries for the single selection of the item and also it gives the duplicate entries on selecting that item again...please help me out to remove the flaws...
如何在单击按钮时将所选项目从一个列表视图复制到另一个列表视图..??在没有任何冗余的情况下,我还可以选择多选项目并批量添加它们而不使用键盘上的 ctrl 吗?使其用户友好我们可以使用复选框以及它们将如何工作?下面的代码用于复制项目的单个选择的条目,它还提供了再次选择该项目的重复条目...请帮我消除缺陷...
private void btn_Add_Click(object sender, EventArgs e)
{
CopySelectedItems(source_name, target_name);
}
private void CopySelectedItems(ListView source, ListView target)
{
foreach (ListViewItem item in source.SelectedItems) {
target.Items.Add((ListViewItem)item.Clone());
}
}
采纳答案by Fredrik M?rk
There are a couple of different ways.
有几种不同的方法。
If you want to copythe items from a to b:
如果要将项目从 a复制到 b:
private static void CopySelectedItems(ListView source, ListView target)
{
foreach (ListViewItem item in source.SelectedItems)
{
target.Items.Add((ListViewItem)item.Clone());
}
}
If you want to movethe items from a to b:
如果要将项目从 a移动到 b:
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);
}
}
Update
You mention that you want to preserve the order in which the items are located in the source ListView
control. I assume that they appear there in some sorted order? If so, you can create a function that uses the same sorting rule to figure out where to insert an item in the target ListView
(my example uses the value in the second column:
更新
您提到要保留项目在源代码ListView
管理中的位置顺序。我假设它们以某种排序顺序出现在那里?如果是这样,您可以创建一个函数,该函数使用相同的排序规则来确定在目标中插入项目的位置ListView
(我的示例使用第二列中的值:
private static void CopySelectedItems(ListView source, ListView target)
{
foreach (ListViewItem item in source.SelectedItems)
{
ListViewItem clone = (ListViewItem)item.Clone();
target.Items.Insert(GetInsertPosition(clone, target), clone); ;
}
}
private static int GetInsertPosition(ListViewItem item, ListView target)
{
const int compareColumn = 1;
foreach (ListViewItem targetItem in target.Items)
{
if (targetItem.SubItems[compareColumn].Text.CompareTo(item.SubItems[compareColumn].Text) > 0)
{
return targetItem.Index;
}
}
return target.Items.Count;
}
It's hard to give a more exact answer without knowing more details.
在不了解更多细节的情况下,很难给出更准确的答案。
回答by Arthur
You have to loop over SelectedItems and create new ListView Items in your second ListView.
您必须遍历 SelectedItems 并在第二个 ListView 中创建新的 ListView Items。
Pseudo code:
伪代码:
foreach(var item in lst1.SelectedItems)
{
var lvi = lst2.Items.Add(item.Text);
lvi.ImageIndex = item.ImageIndex;
...
}
回答by Jamie Keeling
I'm going to hazard a guess that it would be something as simple as saving all the selected items from the first listView into a list of the correct type and then iterating through that list to add them all to the second listView?
我要冒险猜测,这很简单,就是将第一个 listView 中的所有选定项目保存到正确类型的列表中,然后遍历该列表以将它们全部添加到第二个 listView?
I'm not at my development computer so I'm afraid I'm not able to post any correct code.
我不在我的开发计算机上,所以恐怕我无法发布任何正确的代码。
回答by Andrew
In the button click handler, find the selected item(s) in the source list and add them to the target list. something like this:
在按钮单击处理程序中,在源列表中找到所选项目并将它们添加到目标列表中。像这样:
var insertPos = 0;
foreach ( ListViewItem s in sourceList.SelectedItems )
{
s.Remove ( );
var copyCode = Int32.Parse ( s.Text );
while ( insertPos < destinationList.Items.Count )
{
var itemAtCandidate = Int32.Parse ( destinationList.Items [ insertPos ].Text );
if ( itemAtCandidate > copyCode )
break;
insertPos++;
}
destinationList.Items.Insert ( insertPos, s );
}
This will move all selected items in "sourceList" to "destinationList" and keep them in sorted order.
这会将“sourceList”中的所有选定项目移动到“destinationList”并按排序顺序保留它们。