如何在 c#net 中单击按钮时将所选项目从一个列表视图复制到另一个列表视图? [英] How to copy the selected items from one listview to another on button click in c#net?

查看:8
本文介绍了如何在 c#net 中单击按钮时将所选项目从一个列表视图复制到另一个列表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在单击按钮时将所选项目从一个列表视图复制到另一个列表视图...?没有任何冗余,我还可以提供多项选择项目的选项,并在不使用键盘上的 ctrl 的情况下批量添加它们吗?使其用户友好我们可以使用复选框以及它们将如何工作?下面的代码用于复制项目的单个选择的条目,并且它还会在再次选择该项目时提供重复的条目......请帮助我消除缺陷......

How can I copy selected items from one listview to another on button click..?? without any redundancy also 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...

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

推荐答案

有几种不同的方法.

如果您想复制项从 a 到 b:

If you want to copy the items from a to b:

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

如果您想移动项从 a 到 b:

If you want to move the items from a to 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);
    }            
}

更新
您提到要保留项目在源 ListView 控件中的位置顺序.我假设它们以某种排序顺序出现在那里?如果是这样,您可以创建一个函数,该函数使用相同的排序规则来确定在目标 ListView 中插入项目的位置(我的示例使用第二列中的值:

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:

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.

这篇关于如何在 c#net 中单击按钮时将所选项目从一个列表视图复制到另一个列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆