none
Visual Basic 2008 How to search items in listview using combobox

    Question

  • Good day!.. I'm having a hard time on how to search items in listview, wherein if I choose items in combo box, it will displays the items that I search. I'm also using database, I have here my output.. Can someone help me?. I will appreciate any suggestions or comments, Thank You and God Bless.. 
    Monday, July 29, 2013 12:12 PM

Answers

  • Im a bit happy now because someone is helping me on my project.. 

    Ji,

    First off, I’m not a database guy – or at least not a ‘heavyweight’ with them. I can make them work (local databases only) for my stuff but it wouldn’t take much to get me lost with them! Just as a warning here.

    As to your situation, you have a number of options. The single biggest reason I’m suggesting the use of a DataGridView is the fact that it supports a DataSource whereas a ListView does not. There are plenty of other built-in amenities that make it worth using too. I have the one in this example set up to look very nearly like a ListView would in Detail view.

    Obviously I don’t have your data so I used my own which is a database of U.S. state and city names along with zip codes (and some other information which I’m not using in this example). The code won’t make much sense until you see the schema of the data; it looks like this:

    As you can see in that, I have a DataSet named “ZCDataSet” and that DataSet has one DataTable called “ZipCodes”. I’ll only be using three of the fields (columns) in this example: StateName, CityName, and ZipCode.

    Since you’re using a database, you might want to have a look at the BindingSource.Filter property. It will do a nice job for you, then to show all of the data again, either remove the filter or set the filter’s string to an empty string.

    That said though, that’s not what I’m doing here. In fact, by default, I don’t have the DataGridView (nor the associated BindingNavigator) set to anything; I’ll do all of that in code.

    I’m making use of LINQ-To-SQL which can be a bit confusing at first, but it’s well worth learning – I encourage you to discover and learn more about it.

    I’ll show the code momentarily but first let me show you what I have set up. The form is laid out like this:

    I have a GroupBox docked to the top. That GroupBox has one CheckBox and a panel. Inside the panel are two ComboBoxes. I’m using a panel to house the two combo’s simply because I can disable the panel and the contents will all become disabled along with it.

    When first started up, it looks like this:

    When the user unchecks the CheckBox, the first combo is both focused and set to automatically drop down:

    When I make a selection from that combo, the second combo is populated (based on what I chose in the first one) and both focused and set to automatically drop down:

    Finally then, when I make my selection there, the data is filtered and shown in the DataGridView:

    Note also that the BindingNavigator will correctly reflect the rows on the DataGridView no matter how it’s filtered (you’ll see why in the code shown momentarily).

    The others work also:

    And when I re-check the CheckBox to view all the data again:

    I’ll show the code here for completeness, but I also have it (better formatted) on a page of my website here.

    Option Strict On Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load ZipCodesTableAdapter.Fill(ZCDataSet.ZipCodes) CheckBox_ViewAll.Checked = True Panel1.Enabled = False With combo_FilterOn .AutoCompleteMode = AutoCompleteMode.SuggestAppend .AutoCompleteSource = AutoCompleteSource.ListItems .DropDownStyle = ComboBoxStyle.DropDownList .Items.AddRange(New String() {"State Name", "City Name", "Zip Code"}) End With With combo_FilterValue .AutoCompleteMode = AutoCompleteMode.SuggestAppend .AutoCompleteSource = AutoCompleteSource.ListItems .DropDownStyle = ComboBoxStyle.DropDownList .Enabled = False End With With DataGridView1 .AllowUserToAddRows = False .AllowUserToDeleteRows = False .MultiSelect = False .SelectionMode = DataGridViewSelectionMode.FullRowSelect .AlternatingRowsDefaultCellStyle.BackColor = Color.LightGreen .RowHeadersVisible = False .Enabled = False End With BindingNavigator1.Enabled = False Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Order By dr.StateName PopulateData(qry.ToList) End Sub Private Sub PopulateData(ByVal rl As List(Of ZCDataSet.ZipCodesRow)) With BindingNavigator1 .BindingSource = Nothing .Enabled = False End With With DataGridView1 .DataSource = Nothing .Enabled = False End With If rl.Count > 0 Then Dim dt As New DataTable Dim column As New DataColumn With column .DataType = System.Type.GetType("System.String") .ColumnName = "State Name" dt.Columns.Add(column) End With column = New DataColumn With column .DataType = System.Type.GetType("System.String") .ColumnName = "City Name" dt.Columns.Add(column) End With column = New DataColumn With column .DataType = System.Type.GetType("System.String") .ColumnName = "Zip Code" dt.Columns.Add(column) End With Dim row As DataRow For Each dr As ZCDataSet.ZipCodesRow In rl row = dt.NewRow row("State Name") = dr.StateName row("City Name") = dr.CityName row("Zip Code") = dr.ZipCode dt.Rows.Add(row) Next Dim bs As New BindingSource bs.DataSource = dt With BindingNavigator1 .BindingSource = bs .Enabled = True End With With DataGridView1 .DataSource = bs .Enabled = True .Columns(0).Width = 150 .Columns(1).Width = 250 .Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill If DataGridView1.RowCount > 0 Then DataGridView1.Rows(0).Selected = True End If End With End If End Sub Private Sub CheckBox_ViewAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles CheckBox_ViewAll.CheckedChanged If CheckBox_ViewAll.Checked Then combo_FilterOn.SelectedIndex = -1 With combo_FilterValue .SelectedIndex = -1 .Items.Clear() .Enabled = False End With Panel1.Enabled = False Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Order By dr.StateName PopulateData(qry.ToList) Else combo_FilterOn.SelectedIndex = -1 With combo_FilterValue .SelectedIndex = -1 .Items.Clear() .Enabled = False End With Panel1.Enabled = True With combo_FilterOn .DroppedDown = True .Focus() End With End If End Sub Private Sub combo_FilterOn_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles combo_FilterOn.SelectedIndexChanged With combo_FilterValue .SelectedIndex = -1 .Items.Clear() .Enabled = False End With If combo_FilterOn.SelectedIndex > -1 Then Dim filterOnValue As String = _ combo_FilterOn.Items(combo_FilterOn.SelectedIndex).ToString Select Case filterOnValue Case "State Name" Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Select dr.StateName Distinct Order By StateName With combo_FilterValue For Each stateName As String In qry .Items.Add(stateName) Next .SelectedIndex = -1 .Enabled = True .DroppedDown = True .Focus() End With Case "City Name" Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Select dr.CityName Distinct Order By CityName With combo_FilterValue For Each cityName As String In qry .Items.Add(cityName) Next .SelectedIndex = -1 .Enabled = True .DroppedDown = True .Focus() End With Case "Zip Code" Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Select dr.ZipCode Distinct Order By ZipCode With combo_FilterValue For Each zc As String In qry .Items.Add(zc) Next .SelectedIndex = -1 .Enabled = True .DroppedDown = True .Focus() End With End Select End If End Sub Private Sub combo_FilterValue_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles combo_FilterValue.SelectedIndexChanged If combo_FilterOn.SelectedIndex > -1 AndAlso _ combo_FilterValue.SelectedIndex > -1 Then Dim filterOnValue As String = _ combo_FilterOn.Items(combo_FilterOn.SelectedIndex).ToString Dim filterFindValue As String = _ combo_FilterValue.Items(combo_FilterValue.SelectedIndex).ToString Select Case filterOnValue Case "State Name" Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Where dr.StateName = filterFindValue _ Order By dr.StateName, dr.CityName, dr.ZipCode PopulateData(qry.ToList) Case "City Name" Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Where dr.CityName = filterFindValue _ Order By dr.StateName, dr.CityName, dr.ZipCode PopulateData(qry.ToList) Case "Zip Code" Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Where dr.ZipCode = filterFindValue _ Order By dr.StateName, dr.CityName, dr.ZipCode PopulateData(qry.ToList) End Select End If End Sub End Class



    I hope that you find this helpful. :)


    Please call me Frank :)

    • Marked as answer by JiThea Tuesday, July 30, 2013 3:52 PM
    Tuesday, July 30, 2013 3:05 PM

All replies

  • Ji,

    First, have you considered using a DataGridView rather than a ListView? I think it's much more adept for your application. That's not to say that what you're asking can't be done in either, just pointing that out.

    Can you explain more about what you want? It's a bit confusing when you talk about the ComboBoxes. I'm thinking that you might mean filtering with "cascading comboboxes" maybe? By that I mean that each subsequent combo's items depend on what the user selected prior to then?

    I'm sure that if you explain it a bit more in-depth that many here can give you some good ideas on how to accomplish your goal.

    :)


    Please call me Frank :)

    Monday, July 29, 2013 7:42 PM
  • Have if I can help, I need to know exactly how you want to look you look for a specific data, first you are working with Stored Procedure or with consultations and in that platform ...? I appreciate to contribute to the idea ...!

    Carlos Israel

    Monday, July 29, 2013 9:03 PM
  • Hello Frank thanks for dropping by.. XD

    I hope this pictures explains what i wanted to explain :D

    but If i choose again another item in the combobox, the listview is blank how can I refresh the data in the listview, I mean if I choose another item it will automatically filter and if i choose again it will again filtered, so on and so fort.. Im very sorry Frank I cant explain it better, Im not really good in english :D

    Tuesday, July 30, 2013 12:46 PM
  • Hello Frank thanks for dropping by.. XD

    I hope this pictures explains what i wanted to explain :D

    but If i choose again another item in the combobox, the listview is blank how can I refresh the data in the listview, I mean if I choose another item it will automatically filter and if i choose again it will again filtered, so on and so fort.. Im very sorry Frank I cant explain it better, Im not really good in english :D

    I think that I understand what you want now. I'd lay it out a bit differently and include a way that the user can "View All Rows" again once it's been filtered.

    Are you opposed to using a DataGridView instead of a ListBox?


    Please call me Frank :)

    Tuesday, July 30, 2013 1:16 PM
  • ...by the way - you misspelled "Engineering".

    I take that a bit personally. ;-)


    Please call me Frank :)

    Tuesday, July 30, 2013 1:17 PM
  • aw Im sorry.. I didn't mean it.. I'm just in a hurry and I didn't checked my spellings XD 
    Tuesday, July 30, 2013 1:37 PM
  • Hello Frank thanks for dropping by.. XD

    I hope this pictures explains what i wanted to explain :D

    but If i choose again another item in the combobox, the listview is blank how can I refresh the data in the listview, I mean if I choose another item it will automatically filter and if i choose again it will again filtered, so on and so fort.. Im very sorry Frank I cant explain it better, Im not really good in english :D

    I think that I understand what you want now. I'd lay it out a bit differently and include a way that the user can "View All Rows" again once it's been filtered.

    Are you opposed to using a DataGridView instead of a ListBox?


    Please call me Frank :)

    I prefer to use Listview Frank coz' Im having a hard time using DataGridView, and I don't know why :D btw I'm using module in my database.. 

    • Edited by JiThea Tuesday, July 30, 2013 1:43 PM wrong grammar
    Tuesday, July 30, 2013 1:40 PM
  • I prefer to use Listview Frank coz' Im having a hard time using DataGridView, and I don't know why :D btw I'm using module in my database.. 

    Give me a little while (I'm at work now) - I'll put an example together using a DataGridView and show you.

    It's not difficult once you get the hang of it. :)


    Please call me Frank :)

    Tuesday, July 30, 2013 1:50 PM
  • I prefer to use Listview Frank coz' Im having a hard time using DataGridView, and I don't know why :D btw I'm using module in my database.. 

    Give me a little while (I'm at work now) - I'll put an example together using a DataGridView and show you.

    It's not difficult once you get the hang of it. :)


    Please call me Frank :)

    Sure sir Frank.. and Thanks for entertaining me :D

    Im a bit happy now because someone is helping me on my project.. 

    Tuesday, July 30, 2013 2:01 PM
  • Im a bit happy now because someone is helping me on my project.. 

    Ji,

    First off, I’m not a database guy – or at least not a ‘heavyweight’ with them. I can make them work (local databases only) for my stuff but it wouldn’t take much to get me lost with them! Just as a warning here.

    As to your situation, you have a number of options. The single biggest reason I’m suggesting the use of a DataGridView is the fact that it supports a DataSource whereas a ListView does not. There are plenty of other built-in amenities that make it worth using too. I have the one in this example set up to look very nearly like a ListView would in Detail view.

    Obviously I don’t have your data so I used my own which is a database of U.S. state and city names along with zip codes (and some other information which I’m not using in this example). The code won’t make much sense until you see the schema of the data; it looks like this:

    As you can see in that, I have a DataSet named “ZCDataSet” and that DataSet has one DataTable called “ZipCodes”. I’ll only be using three of the fields (columns) in this example: StateName, CityName, and ZipCode.

    Since you’re using a database, you might want to have a look at the BindingSource.Filter property. It will do a nice job for you, then to show all of the data again, either remove the filter or set the filter’s string to an empty string.

    That said though, that’s not what I’m doing here. In fact, by default, I don’t have the DataGridView (nor the associated BindingNavigator) set to anything; I’ll do all of that in code.

    I’m making use of LINQ-To-SQL which can be a bit confusing at first, but it’s well worth learning – I encourage you to discover and learn more about it.

    I’ll show the code momentarily but first let me show you what I have set up. The form is laid out like this:

    I have a GroupBox docked to the top. That GroupBox has one CheckBox and a panel. Inside the panel are two ComboBoxes. I’m using a panel to house the two combo’s simply because I can disable the panel and the contents will all become disabled along with it.

    When first started up, it looks like this:

    When the user unchecks the CheckBox, the first combo is both focused and set to automatically drop down:

    When I make a selection from that combo, the second combo is populated (based on what I chose in the first one) and both focused and set to automatically drop down:

    Finally then, when I make my selection there, the data is filtered and shown in the DataGridView:

    Note also that the BindingNavigator will correctly reflect the rows on the DataGridView no matter how it’s filtered (you’ll see why in the code shown momentarily).

    The others work also:

    And when I re-check the CheckBox to view all the data again:

    I’ll show the code here for completeness, but I also have it (better formatted) on a page of my website here.

    Option Strict On Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load ZipCodesTableAdapter.Fill(ZCDataSet.ZipCodes) CheckBox_ViewAll.Checked = True Panel1.Enabled = False With combo_FilterOn .AutoCompleteMode = AutoCompleteMode.SuggestAppend .AutoCompleteSource = AutoCompleteSource.ListItems .DropDownStyle = ComboBoxStyle.DropDownList .Items.AddRange(New String() {"State Name", "City Name", "Zip Code"}) End With With combo_FilterValue .AutoCompleteMode = AutoCompleteMode.SuggestAppend .AutoCompleteSource = AutoCompleteSource.ListItems .DropDownStyle = ComboBoxStyle.DropDownList .Enabled = False End With With DataGridView1 .AllowUserToAddRows = False .AllowUserToDeleteRows = False .MultiSelect = False .SelectionMode = DataGridViewSelectionMode.FullRowSelect .AlternatingRowsDefaultCellStyle.BackColor = Color.LightGreen .RowHeadersVisible = False .Enabled = False End With BindingNavigator1.Enabled = False Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Order By dr.StateName PopulateData(qry.ToList) End Sub Private Sub PopulateData(ByVal rl As List(Of ZCDataSet.ZipCodesRow)) With BindingNavigator1 .BindingSource = Nothing .Enabled = False End With With DataGridView1 .DataSource = Nothing .Enabled = False End With If rl.Count > 0 Then Dim dt As New DataTable Dim column As New DataColumn With column .DataType = System.Type.GetType("System.String") .ColumnName = "State Name" dt.Columns.Add(column) End With column = New DataColumn With column .DataType = System.Type.GetType("System.String") .ColumnName = "City Name" dt.Columns.Add(column) End With column = New DataColumn With column .DataType = System.Type.GetType("System.String") .ColumnName = "Zip Code" dt.Columns.Add(column) End With Dim row As DataRow For Each dr As ZCDataSet.ZipCodesRow In rl row = dt.NewRow row("State Name") = dr.StateName row("City Name") = dr.CityName row("Zip Code") = dr.ZipCode dt.Rows.Add(row) Next Dim bs As New BindingSource bs.DataSource = dt With BindingNavigator1 .BindingSource = bs .Enabled = True End With With DataGridView1 .DataSource = bs .Enabled = True .Columns(0).Width = 150 .Columns(1).Width = 250 .Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill If DataGridView1.RowCount > 0 Then DataGridView1.Rows(0).Selected = True End If End With End If End Sub Private Sub CheckBox_ViewAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles CheckBox_ViewAll.CheckedChanged If CheckBox_ViewAll.Checked Then combo_FilterOn.SelectedIndex = -1 With combo_FilterValue .SelectedIndex = -1 .Items.Clear() .Enabled = False End With Panel1.Enabled = False Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Order By dr.StateName PopulateData(qry.ToList) Else combo_FilterOn.SelectedIndex = -1 With combo_FilterValue .SelectedIndex = -1 .Items.Clear() .Enabled = False End With Panel1.Enabled = True With combo_FilterOn .DroppedDown = True .Focus() End With End If End Sub Private Sub combo_FilterOn_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles combo_FilterOn.SelectedIndexChanged With combo_FilterValue .SelectedIndex = -1 .Items.Clear() .Enabled = False End With If combo_FilterOn.SelectedIndex > -1 Then Dim filterOnValue As String = _ combo_FilterOn.Items(combo_FilterOn.SelectedIndex).ToString Select Case filterOnValue Case "State Name" Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Select dr.StateName Distinct Order By StateName With combo_FilterValue For Each stateName As String In qry .Items.Add(stateName) Next .SelectedIndex = -1 .Enabled = True .DroppedDown = True .Focus() End With Case "City Name" Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Select dr.CityName Distinct Order By CityName With combo_FilterValue For Each cityName As String In qry .Items.Add(cityName) Next .SelectedIndex = -1 .Enabled = True .DroppedDown = True .Focus() End With Case "Zip Code" Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Select dr.ZipCode Distinct Order By ZipCode With combo_FilterValue For Each zc As String In qry .Items.Add(zc) Next .SelectedIndex = -1 .Enabled = True .DroppedDown = True .Focus() End With End Select End If End Sub Private Sub combo_FilterValue_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles combo_FilterValue.SelectedIndexChanged If combo_FilterOn.SelectedIndex > -1 AndAlso _ combo_FilterValue.SelectedIndex > -1 Then Dim filterOnValue As String = _ combo_FilterOn.Items(combo_FilterOn.SelectedIndex).ToString Dim filterFindValue As String = _ combo_FilterValue.Items(combo_FilterValue.SelectedIndex).ToString Select Case filterOnValue Case "State Name" Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Where dr.StateName = filterFindValue _ Order By dr.StateName, dr.CityName, dr.ZipCode PopulateData(qry.ToList) Case "City Name" Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Where dr.CityName = filterFindValue _ Order By dr.StateName, dr.CityName, dr.ZipCode PopulateData(qry.ToList) Case "Zip Code" Dim qry = From dr As ZCDataSet.ZipCodesRow In ZCDataSet.ZipCodes _ Where dr.ZipCode = filterFindValue _ Order By dr.StateName, dr.CityName, dr.ZipCode PopulateData(qry.ToList) End Select End If End Sub End Class



    I hope that you find this helpful. :)


    Please call me Frank :)

    • Marked as answer by JiThea Tuesday, July 30, 2013 3:52 PM
    Tuesday, July 30, 2013 3:05 PM
  • Ooohh My God!..

    Sir this is exactly what I want.. You explained it very clearly.. That output you suggested is what I'm trying to work.. It is very helpful sir, it is very helpful.. 

    And now my problem is, how to learn the DataGridView.. :D

    Thank You so much sir Frank.. I'll try to study your codes and outputs.. You're a Life Saver sir.. :D :D :D


    • Edited by JiThea Tuesday, July 30, 2013 3:55 PM wrong grammar
    Tuesday, July 30, 2013 3:47 PM
  • Sir Frank? :)

    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.

    Tuesday, July 30, 2013 3:49 PM
  • Ooohh My God!..

    Sir this is exactly what I want.. You explained it very clearly.. That output you suggested is what I'm trying to work.. It is very helpful sir, it is very helpful.. 

    And now my problem is, how to learn the DataGridView.. :D

    Thank You so much sir Frank.. I'll try to study your codes and outputs.. You're a Life Saver sir.. :D :D :D


    It's a lot to take in at once, but go slowly - it's worth learning.

    I'm glad that it helped!


    Please call me Frank :)

    Tuesday, July 30, 2013 4:18 PM