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

问题描述

我如何从一个列表视图复制所选项目到另一个按钮,点击..? 没有任何冗余也可以,我给多个选择项目的选项,而无需使用来自键盘CTRL增加他们在一个大容量?使得用户可以方便我们使用复选框以及他们如何工作的?在code以下用于复制的条目为单一的项目的选择,也给它再次选择该项目的重复项......请帮助我,以消除缺陷...

 私人无效btn_Add_Click(对象发件人,EventArgs的)
{
    CopySelectedItems(SOURCE_NAME,target_name的);
}

私人无效CopySelectedItems(ListView的来源,ListView的目标)
{
    的foreach(在source.SelectedItems ListViewItem的项目){
        target.Items.Add((ListViewItem的)item.Clone());
    }
}
 

解决方案

有几个不同的方式。

如果你想的复制的从项目到B:

 私有静态无效CopySelectedItems(ListView的来源,ListView的目标)
{
    的foreach(在source.SelectedItems ListViewItem的项目)
    {
        target.Items.Add((ListViewItem的)item.Clone());
    }
}
 

如果你想的移动的从项目到B:

 私有静态无效MoveSelectedItems(ListView的来源,ListView的目标)
{
    而(source.SelectedItems.Count大于0)
    {
        ListViewItem的TEMP = source.SelectedItems [0];
        source.Items.Remove(临时);
        target.Items.Add(临时);
    }
}
 

更新
你提到要preserve在该项目所在的源的ListView 控制顺序。我认为他们在那里出现在一些有序?如果是这样,你可以创建一个使用相同的排序规则,以找出其中在目标插入项功能的ListView (我的例子中使用第二列中的值:

 私有静态无效CopySelectedItems(ListView的来源,ListView的目标)
{
    的foreach(在source.SelectedItems ListViewItem的项目)
    {
        ListViewItem的克隆=(ListViewItem的)item.Clone();
        target.Items.Insert(GetInsertPosition(克隆,目标),克隆); ;
    }
}

私有静态诠释GetInsertPosition(ListViewItem的项目,ListView的目标)
{
    const int的compareColumn = 1;
    的foreach(ListViewItem的targetItem在target.Items)
    {
        如果(targetItem.SubItems [compareColumn] .Text.CompareTo(item.SubItems [compareColumn]。文)大于0)
        {
            返回targetItem.Index;
        }
    }
    返回target.Items.Count;
}
 

这很难给出一个更准确的答案不知道更多的细节。

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());
    }
}
解决方案

There are a couple of different ways.

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

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

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#中网?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 16:38