EF in WinForms: how to filter data in BindingSource/DGW (.Local.ToBindingList())

ado.net c# entity-framework entity-framework-6 winforms

Question

Following this instruction, I created an EF model (Database first) and DataSource.

I made BindingSource (bsUsers) for my form and bound DataGridView to it.

Here is how I import data when a form first launches:

    _myDbContext = new MyDbContext();

    _myDbContext.Users.Load();

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

Using DataGridView and other bound controls, I can add and modify records.

However, I was unable to determine how to filter the data. For instance, I'd like a textbox where I can enter a user name (or a portion of one) and have all other entries hidden.

It appears that filtering is not supported by the BindingList returned by the ToBidingList() method.

I did it.

bsUsers.Filter = "Id = 1";

or

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

No impact, though. the number 46, andbsUsers.SupportsFiltering is false .

I only found this issue on Google, MSDN subject, where they advise filtering data before loading (Users.Where(...).Load() ), and I don't require that. I want to import every record and then let the user filter it.

1
2
4/26/2014 10:35:54 AM

Accepted Answer

The Filter possession of theBindingSource only effective with lists that use theIBindingListView interface. BecauseBindingList just equipmentIBindingList (which is incapable of screening)Filter property has no effect.

Alternatively, import your data into aList<User> as well as the bsUsers. DataSource contains the list's filtered items, such asusers.Where( u=> u.Id == 1 ).ToList() You may also use a list that implements IBindingListView in instead of your BindingList. Here are some possible arrows: Implementations of IBindingListView in general

2
5/23/2017 11:46:04 AM






Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow