Question Filtering my DataGridView with Combo Boxes

Danielsm97

New member
Joined
Feb 23, 2015
Messages
1
Programming Experience
Beginner
Hello everyone! I have recently started working with windows form application and I must say it is really awsome! I have came up with a project but I came across a little problem and I hope some of you might help me!
I have established a local database and managed to connect it to a DataGridView, awsome I can see my database in my windows form! I have also made 7 combo boxes, each with its strings in it, then i added a button that should filter the data grid view according to the strings the user selected in the combo boxes. My question is, how do i do that? I mean how do i make a button that when i press it it will filter my DataGridView according to the strings selected in all the combo boxes?

here is a picture, i hope it makes things more clear:
Untitled1.png

Sorry for my bad english and for my lack of knowledge in windows form application but i hope someone could help me!
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,279
Location
Sydney, Australia
Programming Experience
10+
Firstly, you need to decide whether you want to retrieve all the data up front and filter locally or retrieve only the filtered data as and when you need it. There are pros and cons for each but it really comes down to how much data you've got and how long it will take to retrieve it when you do. If you have a lot of data then it's going to take a reasonable amount of time to retrieve that up front but each filter action will be quite quick. If the user is only going to view a small amount of the total data though, you have wasted some of that retrieval time. If you choose to retrieve data as and when you need it then there will be no wait at the start but there will be a small wait each time. If the user will only view a small proportion of the data then the multiple small waits may well be better than the one big wait. If they'll view most or all of the data though, the multiple small waits may end up being less desirable than the one big wait. Also, you may end up retrieving the same data multiple times.

Assuming that you choose to retrieve all the data up front, filtering is easiest if you are using a DataTable. Assuming that that's what you're doing, you should bind that DataTable to a BindingSource and bind that to your grid. You can then filter the data simply by setting the Filter property of the BindingSource, e.g.
myBindingSource.Filter = String.Format("Column1 = {0} AND Column2 = {1}", ComboBox1.SelectedValue, ComboBox2.SelectedValue);
If you're going to retrieve data on demand then the details will change depending on whether you're using a typed or untyped DataSet, i.e. used the wizard or wrote your own adapters.
 
Top Bottom