vb.net 使用 BindingSource (Of CustomClassObjects) 的 DataGridView 过滤
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18254618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridView Filtering Using BindingSource (Of CustomClassObjects)
提问by Alex
I want to filter through my DataGridView data. My DataGridView's DataSource is bound to a BindingSource. The BindingSource contains a list of objects from my clsBillHeaderclass.
我想过滤我的 DataGridView 数据。我的 DataGridView 的 DataSource 绑定到一个BindingSource. BindingSource 包含我的clsBillHeader类中的对象列表。
Here is the first bit of code:
这是第一部分代码:
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
This successfully returns all my values needed as you can see in this screenshot:
这成功地返回了我需要的所有值,如您在此屏幕截图中所见:


The problem is it doesn't filter through anything ... my JobNumber Like '100%'doesn't seem to be filtering at all. As seen below:
问题是它没有过滤任何东西......我的JobNumber Like '100%'似乎根本没有过滤。如下图所示:


I should just be getting the first two numbers but it returns everything else ...
我应该只得到前两个数字,但它返回所有其他数字......
Why am I not using DataView to use filtering?
为什么我不使用 DataView 来使用过滤?
Well, DataView would cause me to use a DataTable that is directly bound to the SQL table. Some values in my table are foreign keys and would need to be transformed into something more meaningful than integers.
好吧,DataView 会导致我使用直接绑定到 SQL 表的 DataTable。我的表中的某些值是外键,需要转换为比整数更有意义的值。
Ex: FK_Author = 1would be the value in the DataTable. Instead, I use my SetByReader to transform it into Author = "Alex". That's why I want to use a BindingSource.
例如:FK_Author = 1将是数据表中的值。相反,我使用我的 SetByReader 将其转换为Author = "Alex". 这就是我想使用 BindingSource 的原因。
All I need is to filter through a DataGridView bound to a class of clsBillHeaders. Has anyone had the same problem?
我所需要的只是过滤绑定到clsBillHeaders. 有没有人遇到过同样的问题?
EDIT
编辑
Take a look at this screenshot. Apparently it doesn't support Filtering ...
看看这个截图。显然它不支持过滤......


Why does my BindingSource variable have this by default?
为什么我的 BindingSource 变量默认有这个?
采纳答案by Sriram Sakthivel
Ah, I understand the problem now. Actually your datasource doesn't support Filtering.
BindingSource.Filterwill work only when your datasource implements IBindingListView
啊,我现在明白问题所在了。实际上您的数据源不支持过滤。
BindingSource.Filter仅在您的数据源实现时才起作用IBindingListView
Only underlying lists that implement the IBindingListView interface support filtering.
只有实现 IBindingListView 接口的底层列表才支持过滤。
So, to make it work you've to change your underlying datasource. If you don't wanna use DataViewfor some reason try using BindingSourceitself as a datasource.
因此,要使其工作,您必须更改基础数据源。如果您DataView出于某种原因不想使用,请尝试将BindingSource其自身用作数据源。
Edit: May this links help how to support filtering http://blogs.msdn.com/b/winformsue/archive/2008/05/19/implementing-filtering-on-the-ibindinglistview.aspxand http://blogs.msdn.com/b/winformsue/archive/2007/12/07/implementing-the-ibindinglistview-for-filtering.aspx
编辑:此链接可能有助于如何支持过滤http://blogs.msdn.com/b/winformsue/archive/2008/05/19/implementing-filtering-on-the-ibindinglistview.aspx和http://blogs。 msdn.com/b/winformsue/archive/2007/12/07/implementing-the-ibindinglistview-for-filtering.aspx
Hope this helps
希望这可以帮助
回答by Jervie Vitriolo
I found a quick and simple solution, I just convert the List to a DataTable, here is the link How to fill a datatable with List<T>
我找到了一个快速简单的解决方案,我只是将列表转换为数据表,这里是链接 如何使用 List<T> 填充数据表

