Creating a list of entities that supports IBinding

2019-09-07 15:24发布

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;
    }

2条回答
欢心
2楼-- · 2019-09-07 15:58

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

查看更多
仙女界的扛把子
3楼-- · 2019-09-07 16:09

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.

查看更多
登录 后发表回答