【问题标题】:How do I filter a BindingSource with a LINQ query as the DataSource如何使用 LINQ 查询作为数据源过滤 BindingSource
【发布时间】:2009-04-23 18:44:30
【问题描述】:

我无法让筛选器在作为 DataGridView 控件的数据源的 BindingSource 上工作。基本上,我有 LINQ 查询,它是 BindingSource 的 DataSource,我想过滤掉结果。以下是我正在尝试完成的示例。

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 切换为使用 DataTable,则过滤将按预期工作。我错过了什么?

【问题讨论】:

    标签: .net data-binding filter


    【解决方案1】:

    来自 BindingSource 文档:

    通常用于复杂的数据绑定 场景中,Filter 属性允许 您可以查看其中的一个子集 数据源。只有基础列表 实现 IBindingListView 接口支持过滤

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 2012-11-27
      相关资源
      最近更新 更多