gpt4 book ai didi

c# - DataGridView 过滤使用 BindingSource(CustomClassObjects)

转载 作者:太空狗 更新时间:2023-10-29 23:08:22 24 4
gpt4 key购买 nike

我想过滤我的 DataGridView 数据。我的 DataGridView 的数据源绑定(bind)到 BindingSource。 BindingSource 包含来 self 的 clsBillHeader 类的对象列表。

这是第一段代码:

Dim bSource As New BindingSource
bSource.DataSource = clsBillHeader.GetAll()
dgvBills.DataSource = bSource
bSource.Filter = "JobNumber Like '100%'" //Filter doesn't actually work

GetAll()

Public Shared Function GetAll() As List(Of clsBillHeader)
Dim mycn As New SqlConnection(connection)
Dim mycmd As New SqlCommand("SELECT * FROM Headers", mycn)
mycn.Open()
Dim myreader As SqlDataReader = mycmd.ExecuteReader
Dim myList As New List(Of clsBillHeader)
While myreader.Read
Dim item As New clsBillHeader()
SetByReader(myreader, item) //Sets all values correctly (such as forein keys)
myList.Add(item)
End While
mycn.Close()
Return myList
End Function

如您在此屏幕截图中所见,这成功返回了我需要的所有值:

BindingSource


问题是它没有过滤任何东西……我的 JobNumber Like '100%' 似乎根本没有过滤。如下所示:

Unwanted Results

我应该只得到前两个数字,但它会返回其他所有内容......

为什么我不使用DataView来使用过滤?

嗯,DataView 会让我使用直接绑定(bind)到 SQL 表的 DataTable。我表中的一些值是外键,需要转换成比整数更有意义的东西。

例如:FK_Author = 1 将是 DataTable 中的值。相反,我使用我的 SetByReader 将其转换为 Author = "Alex"。这就是我想要使用 BindingSource 的原因。

我只需要过滤绑定(bind)到 clsBillHeaders 类的 DataGridView。有没有人遇到同样的问题?

编辑

看看这个截图。显然它不支持过滤......

NoFilter Support

为什么我的 BindingSource 变量默认有这个?

最佳答案

啊,我现在明白问题所在了。实际上您的数据源不支持过滤。 BindingSource.Filter仅当您的数据源实现 IBindingListView

时才会起作用

Only underlying lists that implement the IBindingListView interface support filtering.

因此,要使其正常工作,您必须更改基础数据源。如果您出于某种原因不想使用 DataView,请尝试使用 BindingSource 本身作为数据源。

编辑:愿此链接有助于如何支持过滤 http://blogs.msdn.com/b/winformsue/archive/2008/05/19/implementing-filtering-on-the-ibindinglistview.aspxhttp://blogs.msdn.com/b/winformsue/archive/2007/12/07/implementing-the-ibindinglistview-for-filtering.aspx

希望对你有帮助

关于c# - DataGridView 过滤使用 BindingSource(CustomClassObjects),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18254618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com