Creating a list of entities that supports IBindingListView?

别来无恙 提交于 2019-12-12 02:58:20

问题


I use the following code in my data repository to return a list of entities. I want to bind to the list using a winforms bindingsource, and then to be able to support and filter the bindingsource

I currently use something like

mybindingSource.datasource =  repository.GetList(p => p.Id > 0 && p.Archived == false, x => x.Organisation);

however mybindingSource.SupportsFilter returns false.

The repository function is

public virtual IList<T> GetList(Func<T, bool> where, params Expression<Func<T, object>>[] navigationProperties)
    {
        List<T> list;
        IQueryable<T> dbQuery = ((DbContext)this.context).Set<T>();

        //Apply eager loading
        foreach (var navigationProperty in navigationProperties)
        {
            dbQuery = dbQuery.Include(navigationProperty);
        }

         list = dbQuery.AsNoTracking().Where(where).ToList();

        return list;
    }

回答1:


You can use BindingListView created by Brian Noyes(author of "Data Binding with Windows Forms 2.0: Programming Smart Client Data Applications with .NET")

or

the BindingListView .NET library

or implement your own. If you have more specific questions, please ask




回答2:


There is a nice article provided by Microsoft for implementing a IBindingListView on a legacy BindingList.

Behind the Scenes: Implementing Filtering for Windows Forms Data Binding

The main mess about this interface, is that the filter property is a string and there is no standard for the format of the expression. So it's up to you to know what you will support (or need to support) as string expression. If you want that "Trump" expression filter on the property "Country" for the value "US", instead of "Country = US" you could do it.

This could become really complex if you want to do a full copy of DataSet expression support.



来源:https://stackoverflow.com/questions/25539970/creating-a-list-of-entities-that-supports-ibindinglistview

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