Click here to Skip to main content
15,881,615 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
int i;
                 for (i = 0; i <= dt.Rows.Count - 1; i++)
                 {

                     listView2.Items.Add(dt.Rows[i].ItemArray[0].ToString());
                     listView2.Items[i].SubItems.Add(dt.Rows[i].ItemArray[1].ToString());



                 }
                 for (i = listView2.Items.Count - 1; i >= 0; i--)
                 {

                     if (listView2.Items.IndexOf(listView2.Items[i].ToString()) < i)

                         listView2.Items.RemoveAt(i);

                 }
Posted
Comments
Member 14188342 5-May-19 12:06pm    
is it write on form_load?

Try something like this:

C#
//tag duplicates for removal
List<item> toRemove = new List<item>();
foreach(Item item1 in listView.Items)
{
  foreach(Item item2 in listView.Items)
  {
    //compare the two items
    if(item1.Tag == item2.Tag)
      toRemove.Add(item2);
  }
}    

//remove duplicates
foreach(Item item in toRemove)
{
  listView.Items.Remove(item);
}</item></item>
 
Share this answer
 
Possibly Duplicate Question...See this Question on code project..Solution also available here

How to remove duplicate items from listView in C#.Net ?[^]


Hope this help...
 
Share this answer
 
v4
Comments
[no name] 20-Feb-15 7:43am    
okk
[no name] 20-Feb-15 7:45am    
i ask about listview not about listbox.
ok
read question carefully and then reply. understand Mr.nav29.
Naveen Kumar Tiwari 20-Feb-15 8:14am    
ok sir.. now is it fine? if it helps mark it as solution..
Depending on what you want to compare, you can use Text or Tag (tag being the better choice).
C#
var tags = new HashSet<object>();
var duplicates = new List<ListViewItem>();

foreach(ListViewItem item in listView.Items)
{
    // HashSet.Add() returns false if it already contains the key.
    if(!tags.Add(item.Tag)
        duplicates.Add(item);
}

foreach(ListViewItem item in duplicates) {
    if (listView.Items.Contains(item)) {
        listView.Items.Remove(item);
    }
}
 
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