Click here to Skip to main content
15,392,442 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a method running synchronously in background. I am getting string from that method each time it executes. I am adding that string in list view. In the list view I created one column. I am adding an item using:

C#
ListViewItem lvi = new ListViewItem();
lvi.text=epc; //epc is the string that I want ti insert in listview
listview1.Items.Insert(0,lvi);


Now I want to avoid the duplicates that I may get in the string 'epc'. As the listbox has lisbox1.contains() property, listview doesn't work on same logic.

What I have tried:

C#
if(!lisetView1.items.contains(lvi))
{
    ListViewItem lvi = new ListViewItem();
}


This logic is not working. I need a solution to avoid duplicates inserting in listview.
Posted
Updated 27-Feb-18 7:16am
v3

The problem is that you are comparing ListViewItem objects which, even if they may have the same text, are distinct objects. Thus, your comparison does not achieve what you expect it to.
You could try
C#
using System.Linq;

// ...

if (!listView1.Items.Any(i => i.Text == epc)) {
   listView1.Items.Add(new ListViewItem(epc));
}


Edit: solution without Linq
Without Linq, you have to resort to old-fashioned iteration over the collection:
C#
bool found = false;
foreach (ListViewItem item in listView1.Items) {
   if (item.Text == epc) {
      found = true;
      break;
   }
}
if (!found) {
   listView1.Items.Add(new ListViewItem(epc));
}
   
v3
Comments
webmail123 27-Feb-18 9:39am
   
Thank you for reply. But I am using Windows 6.0 SDK mobile platform. I can not use System.Linq. Neither I can use .Any property for listView class.
In short I can not use listView1.Items.Any property.
phil.o 27-Feb-18 9:52am
   
Please see my updated answer.
webmail123 27-Feb-18 13:52pm
   
Thank you very much for the answer.
phil.o 27-Feb-18 13:56pm
   
You are welcome.
You could simply use a list (of string). stringList.Contains(epc) will work the way you are expecting.
   
Comments
webmail123 27-Feb-18 13:48pm
   
Thank you for reply. But I didn't get clearly what you are saying. Can you provide a sample code so that I will get clear idea.?
kmoorevs 27-Feb-18 14:59pm
   
//scope is same level as listView1
private List<string> strList = new List<string>();

When you add an item to listView1, also add the item to strList, but only after making sure it doesn't already exist in the list. (you were on the right track earlier) Instead of checking for the value in the listView Items collection, check against the generic list.
   if (! strList.Contains(epc))
      {
          listView1.Items.Add(new ListViewItem(epc));

           strList.Add(epc);
       }

The downside to this solution is that you have to maintain the list, which is easy enough. When you remove or even edit an item in the listview, make sure you also remove or edit from the list. Here's to remove selected items:
foreach (ListViewItem l in listView1.SelectedItems)
{
    strList.Remove(l.Text);
    l.Remove();
}

Another hack I've used before is keeping a delimited string. That would look like this:
private string delimitedString = "";

//when adding item(s) to listview
            if (!delimitedString.Contains("|" + epc + "|"))
            {
                listView1.Items.Add(new ListViewItem(epc));
                if (listView1.Items.Count == 1)
                {
                    delimitedString = "|";
                }
                delimitedString += epc  + "|";
            }
//when removing item(s) also remove from the delimited string like this:
            foreach (ListViewItem l in listView1.SelectedItems)
            {
                delimitedString = delimitedString.Replace("|" + l.Text + "|", "|");
                l.Remove();
            }
webmail123 28-Feb-18 9:33am
   
Hey.. Thank you very much. By this I actually avoided the loop which will help me to increase the performance. Thank you very much for your input. :-)

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