Filtering a binding list, binding doesn't work

萝らか妹 提交于 2021-01-28 11:53:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!