我如何筛选的BindingSource与LINQ查询作为数据源数据源、BindingSource、LINQ

2023-09-04 09:34:42 作者:白胡子大佬

我遇到了麻烦一个过滤器,工作在一个BindingSource的是数据源的DataGridView控件。基本上,我有LINQ查询的数据源的BindingSource的,我想过滤下来的结果。下面是什么,我试图完成一个例子。

I'm having trouble getting a filter to work on a BindingSource that is the DataSource for a DataGridView control. Basically, I have LINQ query that is the DataSource for the BindingSource and I would like to filter down the results. Below is an example of what I'm trying to accomplish.

Dim query = From row In dataTable _
            Select New MyRow(row)

Dim bs As New BindingSource()
bs.DataSource = query.ToList()

grid.DataSource = bs

bs.Filter = "Col1 = 'value'"

...

Public Class MyRow
    Private _key As String
    Private _col1 As String

    Public Sub New(ByVal row As DataTableRow)
        _key = GetNewKeyValue()
        _col1 = row.Col1
    End Sub

    Public ReadOnly Property Key() As String
        Get
            Return _key
        End Get
    End Property

    Public ReadOnly Property Col1() As String
        Get
            Return _col1
        End Get
    End Property
End Class

所以,我可以看到所有的行,在DataGridView控制,但过滤器不产生任何影响。如果我切换的BindingSource的DataSource使用一个数据表,然后将过滤工作正常。我在想什么?

So, I can see all the rows in the DataGridView control but the filter doesn't have any effect. If I switch the BindingSource's DataSource to use a DataTable, then the filtering works as expected. What am I missing?

推荐答案

从BindingSource的文档:

From the BindingSource documentation:

典型地,在使用复杂的数据绑定   方案中,过滤器属性允许   您查看的一个子集   数据源。只有底层列出了   实施IBindingListView   接口支持过滤

Typically used in complex data-binding scenarios, the Filter property allows you to view a subset of the DataSource. Only underlying lists that implement the IBindingListView interface support filtering