Link to home
Start Free TrialLog in
Avatar of bng0005
bng0005

asked on

Filtering dataset based off combo box selection

Here's what I'm trying to do.

When my application starts, it retrieves the data from an SQL Server 2000 database, populates the dataset, displays to the user. Now I want to allow the user the ability to filter the data based off a combobox criteria, but not sure how to start the code for that.

Not sure if I need to create a new dataset based off the filter criteria or if there is a way to filter my existing dataset. Any help is appreciated, and if you need further explanation, just ask.



Bryan
Avatar of heintalus
heintalus

Take a look at using DataViews & the DataViewManager.  It will allow you to Filter & sort the data in your existing dataset without the need to create a new dataset.

HTH
Andy
Hi there.. use this..

For WebPage:
        Dim dv As New DataView(ds.Tables(0))
        dv.RowFilter = "Dept=" & DropDownList.SelectedItem.ToString()
        MyDataGrid.DataSource = dv
        MyDataGrid.DataBind()

For Windows Apln:
        Dim dv As New DataView(ds.Tables(0))
        dv.RowFilter = "Dept=" & ComboBox1.Text
        MyDataGrid.DataSource = dv

-Baan
Avatar of bng0005

ASKER

Ok read up on dataviews some, now for another question.

I want to filter the data based on who has what type of card, my filter options are the names of the cards(Restricted Access, General, etc.),  so I need to filter the datatable based on if the field has a value.

set up the code like this so far,

Private Sub cboFilter_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFilter.SelectedIndexChanged
        Dim dv As DataView = New DataView(DsMain1.Tables("MainData"))

        dv.RowFilter =
    End Sub

Not too sure about what should be on the right of the = .  Gonna look around more on dataviews and see what I can find. Thanks for the help so far.


Bryan
Avatar of bng0005

ASKER

above post meant to say

So I need to filter the datatable based on if the card number field has a value
dv.RowFilter = "FIELDNAME = VALUE"

eg:  
If Table has the following columns:
 
Column Name: USER        |        NAME         |     ACCESS        |
Row 1:            RMOORE   | Rodney Moore    |     Restricted     |
Row 2:            TALLAN     | Tom Allan          |     General        |
Row 3:            MHANSON | Mark Hanson      |     Restricted     |

and you wanted to subset to all restricted then your code would be:

dv.RowFilter = "ACCESS = Restricted"

If you wanted to subset to RMOORE then your code would be:

dv.RowFilter = "USER = RMOORE"



Hope this helps


Hi there...
use,

dv.RowFilter = "cardnumber IS NOT Null"

-Baan
Avatar of bng0005

ASKER

Ok, code looks like this now:

Private Sub cboFilter_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFilter.SelectedIndexChanged
        Dim dv As DataView = New DataView(DsMain1.Tables("MainData"))

        dv.RowFilter = "CardNo_387 IS NOT NULL"
    End Sub

Now how would I apply this to my dataset, because it's not changing anything right now. Sorry for the dumb questions, couldn't find any good examples of dataviews on the net so far



Bryan
Don't worry.. it is not at all a dumb question.

>>>"Now how would I apply this to my dataset"<<< This is a wrong conception you are in. By filtering we are NOT changing anythin in dataset. The table in the dataset will still be the same. What we are changing here, is the way we are taking the datafrom the dataset table, into the View (View is NOT a table , but a virtual table).

ok.. let me know something... how are you showing it to the user... I mean how are you displaying the data ?

If you are using a DataGrid , follow my first post with the filter in it.

-Baan
Avatar of bng0005

ASKER

Displaying my data through text boxes bound to the dataset. Couldn't use a datagrid because of the purpose of the application
Bind your text box's to the dataview
How are you binding your data to TextBoxes , regardless of the filtering ..? Post ur code (binding part) here.

-Baan
Avatar of bng0005

ASKER

I was using the databinding.text in the properties menu for each textbox, but looks like I'll have to change that.

Is it a windows application or a webpage, you are working on ??
How are you showing multiple rows of data ??

-Baan
Avatar of bng0005

ASKER

Ok I switched over some text boxes to test it out. My binding code is this:

Private Sub BindControls(ByVal sender As Integer)

   txtFirstName.Text = dr.FirstName
   txtLastName.Text = dr.LastName

End Sub


This function is called on form load and record change event. Works perfectly when using only the dataset. The integer is the datarow number from my dataset. Might have to change this code all around though because not sure how to determine if the code is being called while using the full dataset or a dataview, though I think the full dataset could be considered the default dataview or something like that.


Bryan
Avatar of bng0005

ASKER

It's a windows app. I don't show multiple rows of data, each record(person) is displayed 1 at a time with all of the information about that record, but since the data can get rather large, I wanted to allow the user to filter results.

So my first line of thinking was create a combo box with the different options to filter by, then when that option is selected, limit the data to only the ones that match the filter criteria. Instead of being able to cycle through 100 records, the user would now only have to cycle through 20.
ok... I got it...
you don't need to change anything...

-Baan
I'll be back in 5 mins...
Avatar of bng0005

ASKER

any update Baan?
Sorry, looks like.. my "5 mins" never ended... well I got too busy in my work... let me go through ur problem again..

-Baan
Avatar of bng0005

ASKER

Anyone else have some suggestions for me to try?
ASKER CERTIFIED SOLUTION
Avatar of DotNetLover_Baan
DotNetLover_Baan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bng0005

ASKER

Ok that worked on a test. Gonna leave the question open for a little longer till I get some time to play around with it some more just in case. Thanks again Baan