Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,
How do I get all different items (not subitems) in a ListView.
For example:
Item1
Item1
Item2
Item2
Item3
And how do I get the count of each different item? The output should look like:
Item1 : 2
Item2 : 2
Item3 : 2
Thanks,
iProgramIt
Posted

If I understand your problem correctly, this is not an user interface problem but related to the data.

I take it the data you have is in some kond of collection or data set. If you need to show distinct items and the amount of each item in your data I'd build a LINQ query to group the data based on the item name and count the amount of items in each group. This way you would get a new list of items with count which you could use as a data source for the listview.

Have a look at Group By Clause (Visual Basic)[^], there's a nice code example, very close to your need.
 
Share this answer
 
Comments
iProgramIt 26-Aug-15 0:26am    
Actually, the data is in a listview. I just need to sort separate items (different names) and count how many occurrences there are under that item.
Wendelius 26-Aug-15 0:39am    
And how the data came into the list view? Can you simply query the collection that is acting as the data source?
iProgramIt 26-Aug-15 0:52am    
Mm...XML. Anyway, I am trying to use
Dim anonym = From Item In ListView1.Items
Group By Item Into Count
ListView2.Items.Clear()
For Each Itm In anonym
Dim lvItem As New ListViewItem(Itm.Item.ToString)
lvItem.SubItems.Add(Itm.Count)
ListView2.Items.Add(lvItem)
Next
But, it actually adds them more than once, instead of grouping them. And the count = 1 for each one. Can you help with that?
First of all, to solve such problems efficiently, you can use data structures based on hash code and buckets, used in .NET BCL for implementation of some collections such as associative arrays. These algorithms offer the time complexity of O(1) for search of "already added item". Please see:
http://en.wikipedia.org/wiki/Associative_array[^],
https://en.wikipedia.org/wiki/Big_O_notation[^],
http://en.wikipedia.org/wiki/Time_complexity[^].

If you did not have to find counts for each item, you would use HashSet; collect items in an instance of a set, checking up if the item is already in a set before adding. I think this should be clear. Please see:
https://msdn.microsoft.com/en-us/library/bb359438%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/bb356440(v=vs.110).aspx[^].

If you also need to calculate counts of occurrences, you need a similar collection based on key-value pair, such as Dictionary. Here is your algorithm: keys should represent your items (not necessarily strings, any objects for which you define equivalence and hash value), values should represent counts. Each time you get new item, you first check if it is already in the collection. But don't use ContainsKey, use TryGetValue, so you can check the present of the item and get the item if it is in the collection. If the item already added, increment the value part corresponding to it in the dictionary, its count. If not, add one with the count of 1. That's all.

Please see:
https://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/kw5aaea4(v=vs.110).aspx[^] (no need to use it),
https://msdn.microsoft.com/en-us/library/bb347013(v=vs.110).aspx[^] (use this for the check).

—SA
 
Share this answer
 
Comments
iProgramIt 26-Aug-15 2:12am    
Does it really have to this complex? Or is there a simpler way? See my comment up the top for some code that isn't working. Hopefully you can solve that, because I feel that method is a simpler method - but it is not working. Thanks -iProgramIt
Sergey Alexandrovich Kryukov 26-Aug-15 9:23am    
Why? If is not complex at all, few lines. And it is much simpler that your own "solution". This is an optimal solution. Moreover, you were supposed to find it by yourself, and to learn how major algorithms work. Please do it and ask your questions if you face any problems. I don't want to "fix" your code, it makes no sense. Look, learn the simple thing: doing your work well.
—SA
iProgramIt 27-Aug-15 4:07am    
Heck, I do much better than the other Year 7's in my class! My classmates cannot even begin to comprehend this stuff. So I am pretty damn smart and I do everything well, even if it is DIFFERENT to your solutions!
Sergey Alexandrovich Kryukov 27-Aug-15 10:21am    
Oh, yes, you are smart as hell, and everyone can see it by your code and comments. :-)
—SA
iProgramIt 29-Aug-15 18:05pm    
Ha-ha. :-|
I simply had to enumerate through an array of values which had a bunch of options in them, enumerate the ListView items, create a dictionary and store keys and values in that. Then the key and value is added to the ListView to get the count:
VB
Private XItems As String() = {"Windows 2000", "Windows 2000 (XBOX)", "Windows XP", "Windows Vista", "Windows 7", _
                                 "Windows 8", "Windows 8.1", "Windows 10", "OS X", "Android", "iOS", "Linux", "Ubuntu", "Other..."}
    Private DICT As New Dictionary(Of String, Integer)
    Private Sub LVLoad()
        DICT.Clear()
        ListView2.Items.Clear()
        For Each ITM As String In XItems
            DICT.Add(ITM, 0)
        Next
        For Each ITM As String In XItems
            For Each XITM As ListViewItem In ListView1.Items
                If XITM.Text = ITM Then
                    DICT(ITM) += 1
                End If
            Next
            ListView2.Items.Add(ITM).SubItems.Add(DICT(ITM))
        Next
    End Sub
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Aug-15 9:27am    
Didn't you say this code is not working? Why posting it as a solution? Worse, why accepting it formally? It looks like fake and is considered as abuse.
—SA
iProgramIt 27-Aug-15 4:05am    
What? Why? It is MY solution in which I designed which actually works! Why don't you try it out? Actually verify what you say and is before posting such a stupid comment!
Sergey Alexandrovich Kryukov 27-Aug-15 10:21am    
Don't be rude. I wrote this only because you said that your code is not working, nothing else.
—SA
iProgramIt 1-Oct-15 5:07am    
Sorry, this is an actual redesign of my code. Not the original one. I apologise for my rudeness.
Sergey Alexandrovich Kryukov 1-Oct-15 10:00am    
All right, thank you.
—SA

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