Navigating a Filtered Dataset with a listbox

Posted on 2004-11-29
Last Modified: 2010-04-23
Ive been searching for the proper way to accomplish this seemingly simple task for several weeks now. Heres it is... I have a dataset loaded and the fields are displayed in text boxes on the form.  To navigate this information I have a listbox that lists one of the fields (in this case it is peoples Names). Clicking on someones name will change the info in all the textboxes to that persons information. Now i had this working by moving to the row of the dataset based on the row selected from the listbox.  This works fine. However, now i want to filter the records displayed in the listbox based on another field. So for example I might want to only display males or only females in the listbox. I am currently filtering this information using the following line:

 ListBox1.DataSource = Members_dataset1.Tables("Members").Select("Gender='Male'", "Name", DataViewRowState.CurrentRows)

This works fine and filters the names shown in the listbox. However, this makes my previous way of navigating using the listbox not function properly, what is happening is the dataset row and the listbox row is not matching up anymore. I am not sure if this is the best way to do this, I dont think accessing the database again using another sql command to get a whole new dataset is the right way. Is there a better way? Thanks!!!
Question by:mgouzie
    LVL 9

    Accepted Solution

    have you tried using a dataview? for example:

    dim dataview1 as dataview

    dataview1 = Members_dataset1.Tables("Members").defaultview

    dataview1.rowfilter = "<your row filter here>"

    this way the row number does not change and you are basically binding to a dataview for the data.
    LVL 19

    Assisted Solution

    Your ListBox was bound to the whole Members_dataset1
    Now after you used the Select method the Listbox now got bound to only the filtered record, so naturally it is now out of sync with the base dataset Members_dataset1.

    Now the first question is why do you want to filter i.e. use the Select method in case you wanto navigate to a particular record you can use Currency Manager and navigate.

    However in case you want to search say 5 records out of the total 10 in the original Dataset and let the user navigate between the newly selected 5 records, and when he wants he can revert back to all the 10 records.
    Now if this is what you want then as mentioned by caball88 you need dataviews.

    have a method called Bindcontrols, now this method will bind all the control to a Dataview, initially this dataview will be created from all the data in the basic dataset

    Then you can filter out the records using Select method and recreate this dataview.

    When you want to revert back to all records again re-create the view from all records in the dataset.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video discusses moving either the default database or any database to a new volume.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now