Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi Everyone ....

I want How to remove duplicate items from listview in C#.Net?

items have already inserted , now i want to remove duplicate items ?

How can i do that ?

Thank You....
Posted
Updated 9-Dec-12 20:46pm
v2

Hi,

Use the below method to delete.
C#
List<item> newList = new List<item>();
List<item> itemsToRemove = new List<item>();
foreach(Item item in listView.Items)
{
  if(newList.Contains(item))
  {
      itemsToRemove.Add(item);
  }
  else
      newList.Add(item);
}    

//Remove duplicate items here
foreach(Item item in itemsToRemove)
{
  listView.Items.Remove(item);
}
 
Share this answer
 
v2
The better method is to check for the duplicate entry before inserting to the listbox.
The other way to remove from the items from the list box is by accessing the
ListBox.Items.Remove property.

Check this too
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/79d430b5-3797-444e-ab8b-7df4a534d7af[^]
 
Share this answer
 
Comments
krushna chandra jena 10-Dec-12 2:42am    
i have already try to check for the duplicate entry before inserting , code was
listView1.FindItemWithText(string)

but it falls , thats why i need a function which behaves like Distinct() method [in List Control ] ?

Is there any method of that kind?
Jibesh 10-Dec-12 2:45am    
Please edit your question from ListBox to ListView . we have varieties of controls in .Net :)
well soon find an answer for that...
Jibesh 10-Dec-12 3:15am    
Unlike the list box, ListView item is not representing one column it might have many columns so its up to you which columns you were checking for duplicate entry.

If the Listview has only one column i guess FindItemWithText will work (havent persoanly tried yet)
try checking this


http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/595ac83b-33aa-4b31-aba9-d0a55ee3e584
The following will hide duplicates, in this case TestName and it's assiciated checkbox as the Listview is Data Bound.

protected void lvConditionTests_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
CheckBox cbIConditionDelete = (CheckBox)e.Item.FindControl("cbIConditionDelete");
cbIConditionDelete.CheckedChanged += new EventHandler(cbIConditionDelete_CheckedChanged);

CheckBox cbITestDelete = (CheckBox)e.Item.FindControl("cbITestDelete");
cbITestDelete.CheckedChanged += new EventHandler(cbITestDelete_CheckedChanged);

Label lblITestName = (Label)e.Item.FindControl("lblITestName");
txtTestName = lblITestName.Text;
if (txtTestName == txtPrevTestName && intCtr > 0)
{
//Hide the Text Name and CheckBox
lblITestName.Visible = false;
cbITestDelete.Visible = false;
}
txtPrevTestName = txtTestName;
intCtr++;

}
}//lvConditionTests_ItemDataBound
 
Share this answer
 

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