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

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

本文介绍了我如何筛选的BindingSource与LINQ查询作为数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了麻烦一个过滤器,工作在一个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  接口支持过滤

这篇关于我如何筛选的BindingSource与LINQ查询作为数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:22