Click here to Skip to main content
11,485,564 members (77,660 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET Application ListView , +
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 9-Dec-12 21:23pm
Edited 9-Dec-12 21:46pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,

Use the below method to delete.
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);
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
krushna chandra jena at 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 at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 380
1 Maciej Los 365
2 Mehdi Gholam 160
3 Raje_ 145
4 Frankie-C 134
0 Sergey Alexandrovich Kryukov 8,819
1 OriginalGriff 7,982
2 Sascha Lefèvre 3,199
3 Maciej Los 2,841
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 20 Jan 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100