vb.net 绑定源过滤
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14102474/
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
Binding source filtering
提问by helios99
Hope you can help me with this.
希望你能帮我解决这个问题。
I'd like to filter my datagridview
by using a certain keyword like a name for example.
I used a data set then bind it to a data source then to my datagridview
for viewing.
例如,我想datagridview
通过使用某个关键字(如名称)来过滤我的。我使用了一个数据集,然后将它绑定到一个数据源,然后绑定到我的datagridview
以供查看。
When I used the bindingsource.filter
I can't get any result.
当我使用时,bindingsource.filter
我无法得到任何结果。
Here is my code:
这是我的代码:
Dim ds As New DataSet
Dim bs As New BindingSource
Dim sql As String = "SELECT TOP 10 * FROM dbo.DimCustomer"
Dim connection As New SqlConnection(sqlconnectionstring)
Dim dataadapter As New SqlDataAdapter(sql, connection)
connection.Open()
ds.Clear()
dataadapter.Fill(ds, "Customer")
connection.Close()
bs.DataSource = ds
dgv1.DataSource = bs
dgv1.DataMember = "Customer"
bs.Filter = "FirstName = 'Jon'"
回答by helios99
Thank you guys for the help but I got it working with the following codes below:
谢谢你们的帮助,但我用下面的代码让它工作:
Dim sql As String = "select * from HumanResources.vEmployee"
Dim connection As New SqlConnection(sqlconnectionstring)
Dim dataadapter As New SqlDataAdapter(sql, connection)
Dim dsView As New DataView()
Try
connection.Open()
ds.Clear()
dataadapter.Fill(ds, "test")
dsView = ds.Tables(0).DefaultView
bs.DataSource = dsView
dgv1.DataSource = bs
bs.Filter = "FirstName like 'J%'"
Catch ex As Exception
MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
Finally
connection.Close()
End Try
回答by Wade73
I believe the issue is that your bindingsource should point at a dataview and not the dataset. Here is the link to the MSDN site with a bit more explanation. Here is more information on the Dataviewas well.
我认为问题在于您的 bindingsource 应该指向数据视图而不是数据集。这是指向 MSDN 站点的链接,其中包含更多解释。这里还有关于Dataview 的更多信息。
I haven't had time to test this, but I believe it is logically correct. Can you try this?
我没有时间对此进行测试,但我相信它在逻辑上是正确的。你能试试这个吗?
dim dsView as new DataView(ds.Tables("Customer"))
bs.DataSource = dsView
dgv1.DataSource = bs
dgv1.DataMember = "Customer"
bs.Filter = "FirstName = 'Jon'"
回答by hosnysys
My name is Mohamed Hosny
我的名字是穆罕默德·霍斯尼
Your code is good and there is no error but i need from you make a couple changes First: add the table name to the dataset bs.DataSource = ds.Tables["DimCustomer"] Second: i think the filter line must come before assign the Binding Source to datagridview
你的代码很好,没有错误,但我需要你做一些改变首先:将表名添加到数据集 bs.DataSource = ds.Tables["DimCustomer"] 其次:我认为过滤器行必须在分配之前出现datagridview 的绑定源
bs.Filter = "FirstName = 'Jon'" dgv1.DataSource = bs
bs.Filter = "FirstName = 'Jon'" dgv1.DataSource = bs
Also i think you don't need the line dgv1.DataMember = "Customer"
另外我认为你不需要 dgv1.DataMember = "Customer" 这一行
Try this and if this doesn't work i will give you the full code but by the c# i make it before many times.
试试这个,如果这不起作用,我会给你完整的代码,但我之前多次使用 c# 制作它。
find me on www.hcsmedia.org or www.hosysys.com
在 www.hcsmedia.org 或 www.hosysys.com 上找到我