Navigating a Filtered Dataset with a listbox

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!!!
mgouzieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

caball88Commented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arif_eqbalCommented:
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.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.