in another topic, I’ve stumbled over this very elegant solution by Darin Dimitrov to filter the DataSource of one ComboBox with the selection of another ComboBox:
how to filter combobox in combobox using c#
combo2.DataSource = ((IEnumerable<string>)c.DataSource)
.Where(x => x == (string)combo1.SelectedValue);
I would like to do a similar thing, but intead of filtering by a second combobox, I would like to filter by the text of a TextBox. (Basically, instead of choosing from a second ComboBox, the user simply enters his filter in to a TextBox). However, it turned out to be not as straight forward as I had hoped it would be. I tried stuff as the following, but failed miserably:
cbWohndresse.DataSource = ((IEnumerable<DataSet>)ds)
.Where(x => x.Tables["Adresse"].Select("AdrLabel LIKE '%TEST%'"));
cbWohndresse.DisplayMember = "Adresse.AdrLabel";
cbWohndresse.ValueMember = "Adresse.adress_id";
ds is the DataSet which I would like to use as filtered DataSource.
“Adresse” is one DataTable in this DataSet. It contains a DataColumn “AdrLabel”. Now I would like to display only those “AdrLabel”, which contain the string from the user input. (Currently, %TEST% replaces the textbox.text.)
The above code fails because the lambda expression does not return Bool. But I am sure, there are also other problems (which type should I use for IEnumerable? Now it’s DataSet, but Darin used String. But how could I convert a DataSet to a string?
Yes, I am as much newbyish as it gets, my experience is “void”, and publicly so. So please forgive me my rather stupid questions.
Your help is greatly appreciated, because I can’t solve this on my own (tried hard already).
Thank you very much!
Pesche
P.S. I am only using Linq to achieve an uncomplicated filter for the ComboBox (avoiding a View). The rest is not based on Linq, but on oldstyle Ado.NET (ds is filled by an SqlDataAdapter), if that’s of any importance.
LINQ does not seem, to me, like an obvious solution here. Your data is already loaded into a
DataSetstructure, so you should be able to do something like this:To address the actual problems in your current code:
dsis of typeDataSet, casting it toIEnumerable<DataSet>will fail.DataTable.Selectreturns an array of rows, not a boolean.Where()call would return zero, one or moreDataSetinstances which each have a table named “Adresse” with at least one row matching the filter. Hence, you end up binding your presentational control to a set ofDataSetinstances, which is not what you need.