I have listbox in my project which is bind to datasource. I would like to be able to filter items only to items matches text i type in additional textbox control. I knew that i might use following DefaultView.RowFilter property as shown below in my old vb.net project. Nevertheless when i try to do the same in my most recent visual studio C# project seems like DefaultView is not anymore recognized. Any thoughts if this was decommised or any other way around how could i filter items?
listbox1.DataSource.DefaultView.RowFilter = "[Nummer] like '%" & lbSearchProd.Text.Trim()
Edit For @dr.null
I've added following line nevertheless i am getting the error below. Note that Datasource contains items.
(cbClient.DataSource as DataTable).DefaultView.RowFilter = "[Value] like '%" + txtFilterClient.Text.Trim() + "%'";
This is how i bind the control:
public void LoadDataToClients(IEnumerable<IdValueReadModel> clients)
{
cbClient.DataSource = clients.ToList();
cbClient.DisplayMember = "Value";
cbClient.ValueMember = "Id";
}
Data is there:


Clarifying the comments to your question above.
First of all
This line works in your old vb.net project because you have had the
Option Strict Offwhich allows implicit narrowing conversions, late binding, and implicit typing that results in anobjecttype. You should not allow the mentioned if you want to write good vb.net code and avoid possible mistakes and bugs by doing so. In c#, you don't have that option. You need to cast theobjectto the type of the assigned object instance to access it.Second of all, your data source here is a
List<T>notDataTable. They are two different things and you can't apply what can be written for one to another. You needLinqto filter aList<T>.Some suggested solutions.
DataTable
Get a
DataTablefrom your data access. Something like:Bind it:
Handle the
TextBox.TextChangedto filter or remove the filter:BindingSource
Like the first solution, get a
DataTablebut bind the control to the multipurposeBindingSource.List<
T>If you need to manipulate a
List<T>and you have a model like:Then you can execute
Linqqueries to filter the data source which it's here of typeList<IdValueReadModel>. You need to cache the original list in a class field to be the data source when you clear the filter. Run aLinqquery to bind a new filtered list.