通过一系列属性对作为一组对象的BindingSource进行排序 [英] Sort a BindingSource which is a set of objects by a series of properties

查看:170
本文介绍了通过一系列属性对作为一组对象的BindingSource进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经问了很多(我知道),但是我无法实现一些对我特别有用的东西.

The question has been asked a lot (which I am aware of) however I have been unable to implement something which works for me specifically.

我正在使用Entity Framework来处理与WinForms应用程序的数据交互(这在概念上有些陌生);这工作正常,意味着任何DataGridView控件的DataSource都是对象列表.

I am using Entity Framework in order to handle data interaction (and am somewhat new to the contept) with a WinForms application; this works fine and means that the DataSource of any DataGridView control is a list of objects.

用户应具有对多列进行排序的能力.对于没有DataSource的DataGridView控件(因为它们是单向的,并且我直接从数据库中提取了数据),这不是问题,因为自定义IComparer类可以处理这种排序.

The user is to have the ability to sort on multiple columns. For DataGridView controls which have no DataSource (as they're one-way and I have pulled the data straight from the DB) this is not an issue, as a custom IComparer class handles this sorting.

但是,我似乎无法对具有DataSource的网格进行排序,其中DataSource是由多列组成的一组对象.

However, I appear to be unable to sort grids which have a DataSource where the DataSource is a set of objects by multiple columns.

要观察的重要一点是,DataGridView的DataSource可以是任何类型的对象(客户,食谱,汽车等)的列表,因为网格可以通过对象表示数据库中任何表中的数据

The important thing to observe is that the DataSource of the DataGridView could be a list of objects of any type (Customers, Recipes, Cars etc.) as the grid could be representing data from any table in the database through objects.

我为DataGridView控件获取DataSource的方式如下(此示例适用于系统中的用户):

The way I am getting the DataSource for the DataGridView control is as follows (this example is for the users in the system):

dtgUserList.pFormGrid.DataEntityProvider.USER_LIST
                    .Where(X => X.USER_ID > 1)
                    .Load();

dtgUserList.pFormGrid.DataSource = dtgUserList.pFormGrid.DataEntityProvider.USER_LIST.Local.ToBindingList();

当DataGridView的DataSource是直接数据库查询的时,我可以简单地将DataGridView的DataSource强制转换为BindingSource,然后将Sort属性设置为排序字符串.例如,如果按USER_NAME排序用户列表,然后按PROFILE排序,则排序如下:

When the DataSource of the DataGridView was a DataTable which was a direct database query, I could simply cast the DataSource of the DataGridView as a BindingSource and then set the Sort property to be the sorting string. For example, if sorting the user list by USER_NAME and then by PROFILE, the sort would be as follows:

((BindingSource)DataSource).Sort = SortArgs;

其中SortArgs ="USER_NAME ASC,PROFILE ASC".只需遍历已排序的列,然后提取并串联该列的DataPropertyName即可确定.

Where SortArgs = "USER_NAME ASC, PROFILE ASC". This could be determined simply by iterating through the columns which were sorted and extracting and concatenating the DataPropertyName of the column.

但是,如果我尝试转换绑定到上述USER_LIST对象集的DataGridView控件的DataSource,则会出现编译时错误,指示无法执行转换.

If I try and cast the DataSource of the DataGridView control which is bound to the above set of USER_LIST objects however, I get a compile time error indicating that the cast cannot be performed.

要尝试解决此问题,我要从现有的DataSource创建一个新的BindingSource,以便尝试并应用相同的排序逻辑(只创建排序字符串,然后将其传递给Sort BindingSource)的属性:

To try and address this, I am creating a new BindingSource from the existing DataSource in order to then try and apply the same sort logic (of just creating the sort string and then passing this to the Sort property of the BindingSource):

BindingSource GridBindingSource = new BindingSource(DataSource, null);
SortedGridBinding.Sort = SortArgs;

不幸的是,当我这样做时,尽管BindingSource实现了IBindingListView,但SupportsAdvancedSorting属性始终是false,这在

Unfortunately, when I do this, the SupportsAdvancedSorting property is always false, despite the BindingSource implementing IBindingListView, which is documented in the MSDN article for this property.

我主要想了解的是为什么尽管我正在使用该类实现所需的接口以允许多列排序,但始终将此属性设置为false.我还看到了许多有关如何通过多个属性对对象列表进行排序的示例,例如本示例但是,除非我误解了某些内容,否则它仍然依赖于强类型IList输入.

What I am primarily trying to understand is why this property is always being set to false, despite the class I am using implementing the required interface to allow multi-column sorting. I have also seen a number of examples on how to implement sorting of lists of objects by multiple properties, such as this example however, unless I am mis-understanding something, this still relies on a strongly typed IList input.

推荐答案

作为一个选项,您可以添加对项目的System.Linq.Dynamic引用,然后在使用System.Linq.Dynamic命名空间后,您可以对IEnumerable<T>进行排序或IQueryable<T>通过将排序列和排序顺序传递为string,例如:

As an option you can add System.Linq.Dynamic reference to your project, then after using System.Linq.Dynamic namespace, you will be able to sort an IEnumerable<T> or IQueryable<T> by passing sort column and sort order as string, for example:

var list = db.Products.ToList();
bindingSource1.DataSource = list.OrderBy("Name ASC, Price DESC").ToList();

资源

  • NuGet软件包.您可以简单地使用以下命令在软件包管理器控制台中安装软件包:Install-Package System.Linq.Dynamic

  • NuGet package for System.Linq.Dynamic. You can install the package simply using this command in package manager console: Install-Package System.Linq.Dynamic

System.Linq.Dynamic

GitHub存储库 >

  • 斯科特Guthrie的博客文章 关于动态LINQ(第1部分:使用LINQ动态查询库)

    这篇关于通过一系列属性对作为一组对象的BindingSource进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆