Question

I generated an EF model (Database first) and DataSource following this tutorial http://msdn.microsoft.com/en-us/data/jj682076.aspx

On my form I created BindingSource (bsUsers) and bound DataGridView to it.

Here is how I load data on form startup:

    _myDbContext = new MyDbContext();

    _myDbContext.Users.Load();

    bsUsers.DataSource = _myDbContext.Users.Local.ToBindingList();

It works, I can add and modify records using DataGridView and other bound controls.

But the thing I didn't figure out is how to filter data. For example I want to have textbox where I can type user name (or part of it) and it will hide all other records.

Looks like BindingList that is returned by ToBidingList() method doesn't support filtering?

I tried this

bsUsers.Filter = "Id = 1";

or

bsUsers.Filter = "Username like 'admin'";

But no effect. UPD: and bsUsers.SupportsFiltering is false.

I have googled but found only this topic MSDN topic where they suggest to filter data before loading (Users.Where(...).Load()), and that's not what I need. I want to load all records and then allow to filter it.

Était-ce utile?

La solution

The Filter property of the BindingSource only works with lists that implement the IBindingListView interface. Since BindingList only implements IBindingList (which has no filtering capabilities) the Filter property does nothing.

Either load your data into something like a List<User> and provide the bsUsers.DataSource with filtered elements of the list, e.g. users.Where( u=> u.Id == 1 ).ToList() or replace your BindingList by a list that implements IBindingListView. Possible pointers from here: Generic IBindingListView Implementations

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top