Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using Windows Forms ListView Controls (ListView1 and ListView2).

pls. refer the screenshot for details.

When I click the button1, then the corresponding list1 selected item will transfer to Listview2.

for example, the first time I selected A, B, C and then click Button1. now the item will move to Listview2 control.

if I select the same item (A, B, C) again then click Button1, now and then the item should not come again in ListView2 controls.

the main concern is, to prevent the duplicate values in listview2.

Refer attached: Listview ScreenShot.jpg

PS: Pls. guide us how to prevent the duplicate values when I try to move the same values in listview2.


What I have tried:

private void Button1_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in Listview1.SelectedItems)
            {
                if (item.Text  != null)
                {
                    Listview2.Items.Add(item.Text);
                }
            }
        }
Posted
Updated 20-Feb-18 2:42am
v2

1 solution

You can do it like this:
foreach (ListViewItem item in listView1.SelectedItems)
{
    if (item.Text != null && !listView2.Items.ContainsKey(item.Text))
    {
        listView2.Items.Add(item.Text, item.Text, null);
    }
}

To remove items from listview2:
foreach (ListViewItem item in listView2.Items)
{
    if (listView2.SelectedItems.Contains(item))
    {
        listView2.Items.Remove(item);
    }
}
 
Share this answer
 
v2
Comments
gani7787 21-Feb-18 1:03am    
Thanks. great help...

it's working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900