问题描述
在网格中,我使用绑定列表来绑定数据:
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();
或者没有过滤器:
childBindingSource.DataSource = db.Child.Local.ToBindingList();
如果我使用where表达式过滤绑定列表,则无法保存数据,但是我没有任何异常.
If I filter the binding list using a where expression, saving data doesn't work,yet i do not get any exceptions.
有什么想法吗?
用于获取子记录的行单击事件:
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();
}
推荐答案
DbSet.Local
是 ObservableCollection
,因此您可以使用 ToBindingList()
,并且使用网格进行数据绑定是一项平滑的操作.
DbSet.Local
is an ObservableCollection
, so you can use ToBindingList()
, and data binding with a grid is a smooth operation.
ToList
只会创建一个 List< T>
,并且数据绑定不会传达对列表的添加和删除(不过我希望更新能够起作用).
ToList
just creates a List<T>
and data binding does not communicate additions and removals from the list (I expect updates to work though).
补救措施是将上下文加载的数据过滤到 Local
列表中:
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.
这篇关于筛选绑定列表,绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!