Results 1 to 3 of 3

Thread: Filter ListView Data Based on Checkbox Selection

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2014

    Filter ListView Data Based on Checkbox Selection

    Hi Team,

    Need your help with a vbform im working on..

    In my form I have a ListView that contains 6 columns of data...

    On top of the ListView I have checkboxes that user will click to filter the data in the ListView...So far I've been able to write to the ListView but unable to filter data based on user selection

    So for example if user clicks on "Employee" checkbox it would filter to only display ListView rows that have "Employee" data in it..

    Can you help me acomplish this?

    Here's the code I have for writing data from txt file:

     Private Sub Shift_Trade_Posting_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim WSHnet As Object
            Dim UserName As String
            Dim UserDomain As String
            Dim objUser As Object
            Dim UserFullName As String
            WSHnet = CreateObject("WScript.Network")
            UserName = WSHnet.UserName
            UserDomain = WSHnet.UserDomain
            objUser = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")
            UserFullName = objUser.FullName
            txt_AgentName.Text = UserFullName
            txt_NTLogin.Text = UserName
            ' Write to ListBox from TextFile
            Dim lv As ListView
            lv = ListView1
            lv.HeaderStyle = ColumnHeaderStyle.Clickable
            lv.Columns.Add("Agent Name", 150)
            lv.Columns.Add("Shift Date", 100)
            lv.Columns.Add("Start Time", 100)
            lv.Columns.Add("End Time", 100)
            lv.Columns.Add("Department", 100)
            lv.Columns.Add("NT Login", 100)
            '// path of text file
            Dim txtFile As String = "C:\Users\user\Desktop\test.txt"
            If File.Exists(txtFile) Then
                Using sr As New StreamReader(txtFile)
                    While Not sr.EndOfStream
                        Dim lineText As String() =
                        If lineText.Length = 6 Then
                            lv.Items.Add(New ListViewItem(lineText))
                        End If
                    End While
                End Using
                MsgBox("Text file not found!")
            End If

    Thank you

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: Filter ListView Data Based on Checkbox Selection

    There's no such thing as filtering in a ListView. An item is either in or out. You would need to actually loop through the items and remove any that you don't want displayed and then add them back in when you do want them displayed. That would mean keeping references to the items in another array or collection so that you can loop through them all easily and also maintain the order.

    Alternatively, do what you probably should have done in the first place and use a DataGridView bound to a BindingSource. As long as the underlying data source supports it, e.g. a DataTable, you can then filter the data by simply setting the Filter property of the BindingSource.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: Filter ListView Data Based on Checkbox Selection

    Actually, as soon as I submitted that last post it occurred to me that you could still make use of a BindingSource because you can still use it to filter even if it's not bound to any control(s). You could populate a DataTable with your data and add an extra column to store a ListViewItem. After setting the Filter of the BindingSource, you could then clear the ListView and repopulate it with all the ListViewItems exposed by the BindingSource.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width