Question Creating multiple filters

Oddism

Member
Joined
Nov 21, 2011
Messages
15
Programming Experience
Beginner
Hi,

Currently, I am using the 'BindingSource.Filter' property to filter a Database and display the filtered data in a DataGridView, here is the code I'm using to filter my columns:

VB.NET:
bs.Filter = cbColumn.Text & " >= '" & cbLevel.Text & "'"

My question is, once I have applied one of these filters, how would I go about filtering the already filtered data. I've tried creating a new 'BindingSource.Filter', however, this replaces the old filter. I have also tried creating multiple BindingSource's which is also resulting in dead ends.
Can someone please explain a way in which this would be possible, or perhaps suggest an alternate, simpler method.

Thanks in advance :)
 
If you want to filter the data on one condition AND and other condition then that's exactly what you do. It's just like an If statement with two conditions: you use And and Or to combine multiple conditions. The same goes here.

Frankly though, there really wasn't a need for you to ask this question. The first place anyone should go when they need help is, not surprisingly, the Help. The MSDN documentation for the BindingSource.Filter property says:
To form a filter value, specify the name of a column followed by an operator and a value to filter on. The accepted filter syntax depends on the underlying data source. If the underlying data source is a DataSet, DataTable, or DataView, you can specify Boolean expressions using the syntax documented for the DataColumn.Expression property.
Follow the link for that property and you will find all the information you would want, including the answer to this question, including an example. Always read the documentation first. Being a beginner is actually more reason to do so, not less.
 
If I ever am stuck with something, the MSDN helpsite is always the first thing which I go to for information. I have read through the BindingSource.Filter property already. I then proceeded to search a variety of other sites looking for a way to make this work yet I'm still having troubles with it.

I'll give an example of what I'm trying to do. Say for instance, a database has just 2 items - 'Age' and 'Hair Colour'.

The user selects the age '25' from a ComboBox containing set values for age - in the ComboBox.SelectedIndexChanged event, a BindingSource.Filter will trigger and display only people aged 25. Below this is a second ComboBox however, this one lists different hair colours, when the user selects a hair colour from here, say blonde, only people who are aged 25 and with blonde hair will appear in the DGV.

Honestly, posting in the forums myself was a last resort, but I've been looking around various sites for hours, trying different things to get this to work but couldn't find anything.
 
You say that you already read the documentation for the BindingSource.Filter and yet you didn't follow that link. I've pointed you towards that link and told you that the answer to your question is there, along with an example, and you apparently still haven't followed that link. I would suggest that you follow that link. In fact, I already did.
 
If you're referring to the DataColumn.Expression property, then I've just read through that and it didn't help with what I'm trying to do. I already have a working filter on my form, it takes the column name from one ComboBox, and the filter value from a second and filters the selected column according to the selected value. My code for that is below, where 'cbColumn' contains various column names and where 'cbLevel' contains numbers which are found within the columns listed in 'cbColumn':
bs.Filter = cbColumn.Text & " >= '" & cbLevel.Text & "'"

This works perfectly and filters my data correctly (which is being displayed in a DataGridView). The issue I'm having trouble with is creating a second filter (which works in exactly the same way as the first), but instead of filtering the entire database, it will only filter the results of what the first filter has found - essentially, one filter within another. Obviously I tried attempting to write another filter using 'bs.Filter' yet this completely disregards the first filter and overwrites it as opposed to further filtering what was being displayed.

Hence, the question which I'm looking for an answer to is how to filter already filtered data.

Again, I apologise if I can find the answer by looking through the MSDN helpsite, but as I said, I did try that and still was unable to figure it out. Regardless, I thank you for your responses so far.
 
Last edited:
You don't create a second filter. There's only one filter. I already told you what to do but you have apparently ignored it: if you want to filter on multiple conditions then you combine them using Boolean logic, i.e. with AND or OR. This is from the documentation that you apparently just read:
Concatenation is allowed using Boolean AND, OR, and NOT operators. You can use parentheses to group clauses and force precedence. The AND operator has precedence over other operators. For example:

(LastName = 'Smith' OR LastName = 'Jones') AND FirstName = 'John'
That is combining three conditions into the one filter expression. There is only ever one filter expression. If you already have a filter expression containing one condition then you simply combine that with another condition with AND or OR between them and then that combined expression replaces the original.
 
Thank you very much for your patience with me, makes perfect sense now. When I read through that page, it was only a skim as I thought you didn't understand what I was trying to do (due to the phrasing of my question). I thought that link was only showing the conditions upon having just a filter on just one item.

Thanks again.
 
Back
Top