Results 1 to 8 of 8

Thread: VB2008 How to search items in listview using combobox

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    36

    VB2008 How to search items in listview using combobox

    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..
    Name:  aw.PNG
Views: 2630
Size:  23.6 KB

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VB2008 How to search items in listview using combobox

    Moved To VB.NET

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    36

    Re: VB2008 How to search items in listview using combobox

    Sir. I'm using Visual Basic 2008 sir, not vb.net

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: VB2008 How to search items in listview using combobox

    So you are using vb.net then. You don't t need to include 2008 or even the .net suffix. 2008 is simply the version.

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    36

    Re: VB2008 How to search items in listview using combobox

    i see.. so can you help me? i have here my codes
    For index As Integer = ListView1.Items.Count - 1 To 0 Step -1
    If ListView1.Items(index).SubItems(1).Text <> cboCollege.Text Then
    ListView1.Items.RemoveAt(index)
    End If
    Next index
    but the problem is, if i try another search, the listview doesn't refresing its list. and the items that I search didn't appear..

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    36

    Re: VB2008 How to search items in listview using combobox

    i see.. so can you help me? i have here my codes
    For index As Integer = ListView1.Items.Count - 1 To 0 Step -1
    If ListView1.Items(index).SubItems(1).Text <> cboCollege.Text Then
    ListView1.Items.RemoveAt(index)
    End If
    Next index
    but the problem is, if i try another search, the listview doesn't refresing its list. and the items that I search didn't appear..

  7. #7
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: VB2008 How to search items in listview using combobox

    Hi,

    i see.. so can you help me? i have here my codes
    For index As Integer = ListView1.Items.Count - 1 To 0 Step -1
    If ListView1.Items(index).SubItems(1).Text <> cboCollege.Text Then
    ListView1.Items.RemoveAt(index)
    End If
    Next index
    but the problem is, if i try another search, the listview doesn't refresing its list. and the items that I search didn't appear..
    You cannot use a ListView in this way. The listView is a standalone collection of ListViewItems and once you remove an item from the ListView it is gone forever until you replace it.

    If you want to continue to use a ListView to display the results of your searches then what you need to do is interrogate the original Data Source in your project to select the records that fit your ComboBox's criteria, clear the existing list of ListViewItems in the ListView and then finally re-add the ListViewItems that match the selected criteria.

    Alternatively, and since you have mentioned that you are using a Database, I would suggest that you create a BindingSource with its DataSource set to the necessary DataTable in your project and then set that BindingSource to the DataSource of a DataGridView.

    You can then use the Filter property of the BindingSource to limit the information displayed in the DataGridView based on the necessary criteria of your ComboBox's.

    For more information on the BindingSource.Filter property, have a look here:-

    http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx

    Hope that helps.

    Cheers,

    Ian

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    36

    Re: VB2008 How to search items in listview using combobox

    Quote Originally Posted by IanRyder View Post
    Hi,



    You cannot use a ListView in this way. The listView is a standalone collection of ListViewItems and once you remove an item from the ListView it is gone forever until you replace it.

    If you want to continue to use a ListView to display the results of your searches then what you need to do is interrogate the original Data Source in your project to select the records that fit your ComboBox's criteria, clear the existing list of ListViewItems in the ListView and then finally re-add the ListViewItems that match the selected criteria.

    Alternatively, and since you have mentioned that you are using a Database, I would suggest that you create a BindingSource with its DataSource set to the necessary DataTable in your project and then set that BindingSource to the DataSource of a DataGridView.

    You can then use the Filter property of the BindingSource to limit the information displayed in the DataGridView based on the necessary criteria of your ComboBox's.

    For more information on the BindingSource.Filter property, have a look here:-

    http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx

    Hope that helps.

    Cheers,

    Ian
    Thanks Ian, but, is it okay to create BindingSource while using this codes in my module?
    Module Module1
    Public cn As ADODB.Connection

    Public rs As ADODB.Recordset

    Public Sub connection()
    cn = New ADODB.Connection
    With cn
    .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Information.accdb"
    .CursorLocation = ADODB.CursorLocationEnum.adUseClient
    .Open()
    End With
    End Sub
    End Module

Tags for this Thread

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