shaik ibrahim
shaik ibrahim

Reputation: 187

Filter a Listview with a combobox in vb.net

What i want is mostly to hide arrays not requeries. So i have a combobox and listbox with values from database done by adapter , dataset , datatable and binding source. . SO when the person change the combobox values it would filter the list .So lets say to combobox is containing the ID so the list view will show those rows with that id. Onload the listview shows all properties . So how do you do this without Requery ???? So below i have the populating list and combobox part.

here is a link but its not very helpfull VB.NET Listview Multiple Column Filter

   Dim listcount As Integer = listview1.Columns.Count

    With listview1
        .Columns.Clear()
        .View = View.Details


        .GridLines = True


        .Columns.Add("Name").Width = 70
        .Columns.Add("ID").Width = 60
        .Columns.Add("Number").Width = 90


    End With


    Try


        strQuery = "Select * From Table"



        DB.Connection = New SqlConnection(strConnection)

        DB.Connection.Open()
        ' add a daaset
       Adapter = New SqlDataAdapter(strQuery, DB.Connection)
        ListRS = New DataSet



        Adapter.Fill(ListRS)

        Dim table As DataTable = ListRS.Tables(0)


        bnsrc = New BindingSource
        bnsrc.DataSource = ListRS.Tables(0)
        combobox1.DataSource = bnsrc
         combobox1.DisplayMember = "ID"
        combobox1.ValueMember = "ID"
        combobox1.AutoCompleteSource = AutoCompleteSource.ListItems

       Listview1.Items.Clear()



        DB.Connection.Close()

So if i we to do requery it would be the same ust change the sql statement .

      strQuery =  "Select * from Table Where ID=@id"

IF i added a parameter how ever it would effect the combobx because they are using the same queries.

Upvotes: 1

Views: 11837

Answers (1)

user2480047
user2480047

Reputation:

You can populate Listview1 on the SelectedIndexChanged event of comboxbox1 by adding the items you want every time. Example:

Private Sub combobox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles comboBox1.SelectedIndexChanged
    ListView1.Items.Clear()
    Dim curSelectionCombobox As String = combobox1.SelectedItem.ToString()
    For Each item As String In allItems
        If (condition) Then
            ListView1.Items.Add(item)
        End If
    Next
End Sub

With the code above, you get the currently-selected item in combobox1 (curSelectionCombobox) and also iterate through all the elements retrieved from the database and decide (on account of the given condition) which ones should/shouldn't be added. Thus, this approach does not use any filtering, but performs a from-scratch addition of elements every time the selection of combobox1 changes.

If you want to perform the "filtering" just at form1 load

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ListView1.Items.Clear()
    For Each item As String In allItems
        If (condition) Then
            ListView1.Items.Add(item)
        End If
    Next
End Sub

If you want to change the items in the Listview1 at runtime based on various conditions, you should rely on controls allowing multiselection (e.g., checkboxes). Sample code:

Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged

    If (CheckBox1.Checked And CheckBox2.Checked And CheckBox3.Checked) Then
        ListView1.Items.Clear()
        For Each item As String In allItems
            If (condition) Then
                ListView1.Items.Add(item)
            End If
        Next
    End If
End Sub

Thus, the idea is not adding all the items to the Listbox1 and then filtering them, but just adding the items you want. Where I put "condition" you can call a function performing as complex checks as you wish.

Upvotes: 1

Related Questions