Hello,

I have a project where I am using a listview to display a data received through XML. One of the columns called "STATUS" display only 2 text data, either "ON" or "OFF". I would like to know how to count how many times the word "ON" it is apearing in that column and same for the word "OFF". I am using this code to count how many lines I have in the listview

Label1.Text = ListView1.Items.Count()

but I couldn't figure out so far how to count of the words "ON" or "OFF" in the column "STATUS" wchich have the index 9.

Any help which will point me to the right direction will be highly apreciated.

Thanks!

Recommended Answers

All 4 Replies

Is your data source queryable? You could try using LINQ. Example:

Dim samples As New List(Of Sample)

        'populate list with Sample objects

        Dim onCount = (From samp In samples Where samp.Status = "On" Select samp).Count()
        Dim offCount = (From samp In samples Where samp.Status = "Off" Select samp).Count()

Unfortunately I cannot query the data source. I need to do this after I retrieve the data in my listview.

Thank you for your help though!

I didn't mean it as running a query against the actual source of the data, more like using LINQ to query the collection of in-memory data. Sorry if that was unclear, or maybe I'm misinterpreting your response.

Anyway, you can query the listView items themselves. Consider this example.

Dim onSwitch As Boolean = True

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'In this example, the listview's main column is called Test Value, the second column is Status
        Dim newItem As ListViewItem = ListView1.Items.Add("test")
        If onSwitch Then
            newItem.SubItems.Add("ON")
            onSwitch = False
        Else
            newItem.SubItems.Add("OFF")
            onSwitch = True
        End If
        Me.Refresh()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        'Status is Index 1 in this example
        Dim onCount = (From item As ListViewItem In ListView1.Items _
                      Where item.SubItems(1).Text = "ON" _
                      Select item).Count()

        Dim offCount = (From item As ListViewItem In ListView1.Items _
                      Where item.SubItems(1).Text = "OFF" _
                      Select item).Count()

        Label1.Text = String.Format("ON: {0} " & vbCrLf & "OFF: {1}", onCount, offCount)

    End Sub

The main code to look at is the button2_click handler. I'm able to use LINQ and extension methods to get a count of items having a specific value (ON or OFF, in this case) at a given position.

You are a genius, Apegram!!!! This is exactly the solution I was looking for.

Thank you very much for your help!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.