views:

245

answers:

2

what is the way to avoid the redundancy in the listview when the items are added to it.. im using winforms c#.net.. i mean how can i compare between the items in listview1 and items in listview2 so that while adding the items from one listview to another it could not enter the items that are already entered in the target listview.. im able to add items from one listview to other but it is adding auplicate items also what is the way to get rid of it..???

A: 

As suggested, hash table is a good way to deter such redundancy.

Kangkan
i know that hash table is the solution but im new to it i dont know how to use it?? can u guide me please...??
zoya
For clarity, I am adding another answer with some sample.
Kangkan
thanks sir for ur help..
zoya
+1  A: 

You can think of something like:

Hashtable openWith = new Hashtable();
// Add some elements to the hash table. There are no 
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// The Add method throws an exception if the new key is 
// already in the hash table.
try
{
    openWith.Add("txt", "winword.exe");
}
catch
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
// ContainsKey can be used to test keys before inserting 
// them.
if (!openWith.ContainsKey("ht"))
{
    openWith.Add("ht", "hypertrm.exe");
    Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]);
}

Now to meet the changes in the problem after edit, you can do it like this:

if(!ListView2.Items.Contains(myListItem))
{
    ListView2.Items.Add(myListItem);
}

You can also refer a similar problem in http://stackoverflow.com/questions/2340439/how-to-add-the-selected-item-from-one-listview-to-another-on-button-click-in-cne

Kangkan