How to copy the selected items from one listview to another on button click in c#net?
How can I copy selected items from one listview to another on button click..?? witho开发者_如何学运维ut 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.
You have to loop over SelectedItems and create new ListView Items in your second ListView.
Pseudo code:
foreach(var item in lst1.SelectedItems)
{
var lvi = lst2.Items.Add(item.Text);
lvi.ImageIndex = item.ImageIndex;
...
}
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?
I'm not at my development computer so I'm afraid I'm not able to post any correct code.
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.
精彩评论