views:

776

answers:

4

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 enteries for the single slection of the item and also it gives the duplicate enteries 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: 

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;
    ...
}
Arthur
what does this part do?? lvi.ImageIndex = item.ImageIndex;
zoya
a ListViewItem has an image index. I suggested that you create new ListViewItems in your destination List. So I added that Property as an example. http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.imageindex(VS.80).aspx
Arthur
A: 

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.

Jamie Keeling
A: 

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.

Andrew
thanks its working good but i need to display them in the sorted order it is displaying the SNo. as such in the source file..i need to reenter the serial order ..what are the specific modifications to be done??
zoya
sir this code is removing the item from one listview and then adding it to other... i dont want to remove the items from one listview i just want to add those to other listview without removing them from the source listview also it should not add the duplicate enteries...
zoya
i just want to copy it..from one to another without redundancy...
zoya
A: 

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.

Fredrik Mörk
thanks for ur code...please tell me if i need to add multiple items them what are the modifications that i can do in the code so that multiple items are selected and added on the destination listview..
zoya
please teell me a way if i could refresh the listview or clear the listview after couple of enteries
zoya
@zoya: as the code samples look, they will handle all different cases (source is empty, source has zero, one or more items selected). There is no need for code modifications to handle these different scenarios.
Fredrik Mörk
sir i didnt understand where this CopySelectedItems event came from?? is this the event of listview??
zoya
sorry my mistake i got it u have made a function..thanks sir i tried it ..it works fine....can u tell me how regenratethe SNo. in the target listview..as it is displaying the SNo. of the source listview..
zoya
@zoya: I updated the answer.
Fredrik Mörk
thank u sir...i have one more thing to ask about it..i have added checkboxes in the listview.. so what should i do to perform the same task for the checked items..ie. multiple items that are checked should be added to the other listview..can u help me out??
zoya
sir i need to tell u something about the code u have given may be it will help u also in ur logic..the logic u r using will put it in the sorted order but..2 the major drawback of this code are 1.it can also give duplicate enteries ie. redundant values 2.it will take the digits in the 1st place.. that is it wil put 11 before 2 .. because it is just recognizing the first digit not the whole no...can u come out of these drawbacks?? i have used one option to avoid these that is checkboxes..but now how to add the checked items to other listview??
zoya
@zoya: I think that you should present the code that you have right now together with the requirements (no duplicates, rules on sorting and so on) into a new question here on SO. I will be easier and clearer to answer like that than to discuss it in the comments here. It will also make it easier for others to locate that information later.
Fredrik Mörk
i have presented the code...just check out that its giving the duplicate enteries on selecting the same item again..that i need to avoid and also i need to give the option for multiple slection of the items not one by one enteries...
zoya