问题
In a grid i use a binding list to bind data:
childBindingSource.DataSource =
db.Child.Local.ToBindingList()
.Where(child => selectedrow != null
&& child.MasterID == selectedrow.ID)
.ToList();
Or without filter:
childBindingSource.DataSource = db.Child.Local.ToBindingList();
If I filter the binding list using a where expression, saving data doesn't work,yet i do not get any exceptions.
Any ideas?
Row click event for getting child records:
private void gridView1_RowClick(object sender, RowClickEventArgs e)
{
selectedrow = gridView1.GetFocusedRow() as Master;
if (selectedrow != null)
{
int id = selectedrow.ID;
db.Child.Where(child => child.MasterID == id).Load();
}
childBindingSource.DataSource = db.Child.Local.ToBindingList();
}
回答1:
DbSet.Local is an ObservableCollection, so you can use ToBindingList(), and data binding with a grid is a smooth operation.
ToList just creates a List<T> and data binding does not communicate additions and removals from the list (I expect updates to work though).
The remedy is to filter the data that the context loads into the Local list:
db.Child.Where(child => selectedrow != null
&& child.MasterID == selectedrow.ID).Load();
childBindingSource.DataSource = db.Child.Local.ToBindingList();
That means that you have to use a new context for each set of child records you show and update.
来源:https://stackoverflow.com/questions/31701480/filtering-a-binding-list-binding-doesnt-work