过滤超出数据集 [英] Filtering beyond DataSets

查看:121
本文介绍了过滤超出数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我已经使用实体框架和绑定了很长一段时间了.突然,以下内容袭击了我:A BindingSource 具有Sort Filter Properties.
仅当绑定Object Implements IBindingListIBindingListView时才支持这些功能.标准List<T>不能Implement 这些. BindingList<T>Implements IBindingList,因此仅支持排序.
那么我如何支持过滤?通过绑定到DataView.
但是,LINQ仅支持ToArrayToDictionaryToList ToLookup.没有ToBindingList ,当然也没有ToDataView.我可以使用ToList 并将List<T>传递给BindingList.要创建DataView ,我将不得不承担更多的麻烦,但是我会设法解决.那么问题出在哪里呢?

假设我有以下代码:

Hi folks,
I have been working with Entity Framework and binding for quite a while now. When suddenly the following struck me: A BindingSource has Sort and Filter Properties.
These are only supported when the bound Object Implements IBindingList or IBindingListView. A standard List<T> does not Implement these. A BindingList<T> only Implements IBindingList and thus only supports Sorting.
So how can I support Filtering? By binding to a DataView.
However, LINQ only supports ToArray, ToDictionary, ToList and ToLookup. No ToBindingList and certainly no ToDataView. I could use ToList and pass the List<T> to a BindingList. To create a DataView I''ll have to take some more trouble, but I''ll manage. So where is the problem?

Let''s just say I have the following code:

BindingSourceCustomers.DataSource = context.Customers.ToList();
BindingSourceOrders.DataSource = BindingSourceCustomers;
BindingSourceOrders.DataMember = "Orders";

让我假设我有一个精通-详细关系,并且两者都在单独的DataGridView中显示.现在,第一个网格将成功显示所有客户,第二个网格现在将显示grid1中所选客户的所有订单.太好了!
除了现在,我只想看到名称以A开头的客户.
请记住,我不能使用BindingSourceFilter Property.但这仍然很容易解决.只需将代码的第一行更改为:

Let''s assume I have a master-detail relation and both are shown in a seperate DataGridView. The first grid would now successfully show all customers and the second grid is now displaying all the orders for the customer selected in grid1. Great!
Except now I only want to see customers whose name starts with A.
Remember that I can''t use the Filter Property of my BindingSource. But this is still easy to fix. Just change the first line of code to:

BindingSourceCustomers.DataSource = context.Customers.Where(c => c.Name.StartsWith("A")).ToList();

但是现在出现了问题.在我的第二个网格中,我只想显示超过100美元的订单.我的BindingSource 正在自动获取每个客户的所有订单.但是,我只想显示订单的选择.

解决此问题的一种方法是,当我单击客户"网格中的一行时,刷新订单的绑定.似乎不是一个很好的解决方案.
另一个解决方案是在我的Customer Object上创建一个Property ,该Property 返回所有超过$ 100的Orders ,但这意味着我的排序选项非常有限,而且所有排序选项在设计时都是已知的. 另一个可能的解决方案(也许)是通过某种方式绑定到IQueryable,尽管我不知道如何执行此操作,或者在这种情况下甚至根本不可能.

当使用DataSets DataTables 时,这都不成问题,因为它们支持对BindingSource 进行过滤(这有点像在之间"绑定之间).我肯定不需要DataSets 支持简单的过滤吗?

我在互联网上搜索,但没有找到明确的解决方案.大多数人并不专注于过滤这样的细节.
除了使用BindingSourceFilter Property之外,我还没有在DataGridView上找到任何过滤选项.
欢迎任何帮助和提示.
谢谢:)

But now comes the problem. In my second grid I only want to show the orders that are more than $100. My BindingSource is getting ALL the orders for each customer automatically. However, I only want to show a selection of the orders.

One way to get around this is by refreshing the bindings for the orders when I click on a row in the customers grid. Seems like a not so elegant solution.
Another solution is to create a Property on my Customer Object that returns all Orders over $100, but that would mean I am very limited in my sorting options and that my sorting options are all known at designtime.
Another possible solution (maybe) is by somehow binding to an IQueryable, although I wouldn''t know how to do this, or if it is even possible at all in this context.

When using DataSets and DataTables all this was no problem because they support filtering on the BindingSource (it''s kind of like getting ''between'' binding). Surely I shouldn''t be needing DataSets to support easy filtering?

I searched on the internet, but did not find a clear solution. Most did not focus on filtering details like this.
I could also not find any filtering options on the DataGridView other than using the Filter Property of the BindingSource.
Any help and tips are welcome.
Thanks :)

推荐答案

100.我的BindingSource 正在自动获取每个客户的所有订单.但是,我只想显示订单的选择.

解决此问题的一种方法是,当我单击客户"网格中的一行时,刷新订单的绑定.似乎不是一个很好的解决方案.
另一种解决方案是在我的Customer Object上创建一个Property ,该Property 返回所有Orders
100. My BindingSource is getting ALL the orders for each customer automatically. However, I only want to show a selection of the orders.

One way to get around this is by refreshing the bindings for the orders when I click on a row in the customers grid. Seems like a not so elegant solution.
Another solution is to create a Property on my Customer Object that returns all Orders over


100,但这将意味着我的排序选项非常有限,并且我的排序选项在设计时都是已知的.
另一个可能的解决方案(也许)是通过某种方式绑定到IQueryable,尽管我不知道如何执行此操作,或者在这种情况下甚至根本不可能.

当使用DataSets DataTables 时,这都不成问题,因为它们支持对BindingSource 进行过滤(这有点像在之间"绑定之间).我肯定不需要DataSets 支持简单的过滤吗?

我在互联网上搜索,但没有找到明确的解决方案.大多数人并不专注于过滤这样的细节.
除了使用BindingSourceFilter Property之外,我还没有在DataGridView上找到任何过滤选项.
欢迎任何帮助和提示.
谢谢:)
100, but that would mean I am very limited in my sorting options and that my sorting options are all known at designtime.
Another possible solution (maybe) is by somehow binding to an IQueryable, although I wouldn''t know how to do this, or if it is even possible at all in this context.

When using DataSets and DataTables all this was no problem because they support filtering on the BindingSource (it''s kind of like getting ''between'' binding). Surely I shouldn''t be needing DataSets to support easy filtering?

I searched on the internet, but did not find a clear solution. Most did not focus on filtering details like this.
I could also not find any filtering options on the DataGridView other than using the Filter Property of the BindingSource.
Any help and tips are welcome.
Thanks :)


我在这里找到了可能的解决方案: ^ ]
I have found a possible solution here : http://www.dotnetmonster.com/Uwe/Forum.aspx/winform-data-binding/1042/BindingSource-Filter-fails-when-datasource-derives[^]


这篇关于过滤超出数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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