Click here to Skip to main content
15,888,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have ListView with GridView(column say IteName, DataType, Value)
now I want to add item to listview But If Item is already present in Listview then I have to give message that say Item already present

I tried but not getting any solution,

Please help me to get out from this.

Thanks
Tink

What I have tried:

C#
foreach (dynamic item in lstSusbscribedGroupTag.Items)
            {
                if (if(lstSubscribeItem.Items.Contains(Item.ToString())))
                    continue;
                else
                    selectedItemCollection.Add(item);
            }
Posted
Updated 23-Mar-17 21:23pm
v2
Comments
CHill60 23-Mar-17 5:24am    
We need to see a bit more of the code that you are using to populate the GridView! Use the Improve question link to provide the missing code
Member 11859517 23-Mar-17 5:34am    
thanks CHill
I added some code as you suggested.

Search the ListView to see if the item already exists, using one of the Find(xxx) methods listed in the documentation[^].
 
Share this answer
 
Another simple solution is to use System.Linq; :
C#
foreach (var item in lstSusbscribedGroupTag.Items.Distinct())
{
    selectedItemCollection.Add(item);
}
If the items are of a complex type, then you need to do a little bit more work. Here are two different solutions for you:

1. Enumerable.Distinct(TSource) Method (IEnumerable(TSource)) (System.Linq)[^] (example #2)
2. c# - LINQ's Distinct() on a particular property - Stack Overflow[^]
 
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