vb.net 如何在数据网格视图上过滤 sql 选择查询的结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14515145/
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
How can I filter the result of sql select query on datagrid view
提问by Juan Filipe
I have a Sql query on my form displaying all records having a duedate of the year 2013 in my tableA filling the datagrid view. And I have also a button and textbox.
我的表单上有一个 Sql 查询,显示在我的 tableA 中填充数据网格视图的所有记录的截止日期为 2013 年。我还有一个按钮和文本框。
What I wanted to do is to filter the result of my sql query on the datagrid view, Is it Possible to have another sql like this:
我想要做的是在数据网格视图上过滤我的 sql 查询的结果,是否有可能有另一个这样的 sql:
"SELECT caseno, duedate,remarks from (the data on my datagrid view)"
"SELECT caseno, duedate,remarks from (the data on my datagrid view)"
this is my datagrid sample:
这是我的数据网格示例:
caseno duedate remarks
1001 1/12/13 passed
1002 1/22/13 passed
1003 1/15/13 failed
1004 1/20/13 none
1005 1/06/13 failed
when I click the button I want to display on my datagrid all the records having a remarks that i typed on my textbox. anyone can help me? tnx! try to make it simple :)
当我单击按钮时,我想在我的数据网格上显示所有带有我在文本框中键入的备注的记录。任何人都可以帮助我吗?天!尽量让它简单:)
回答by bonCodigo
Solution 1:
解决方案1:
- Set datasource of datagridview to select sql query
- Bind data only when text change event occurs
- Do a validation on the textbox text on button click event
- 设置datagridview的数据源选择sql查询
- 仅在文本更改事件发生时绑定数据
- 对按钮单击事件的文本框文本进行验证
Reference: How to filter records in datagrid view and show the selected record in the datagrid
参考:如何在数据网格视图中过滤记录并在数据网格中显示所选记录
Solution 2:
解决方案2:
Check on this post on codeproject for Filter DataGridview with data entered in textbox
在codeproject上查看此帖子以使用在文本框中输入的数据过滤 DataGridview
The important point to notice that you can use DataView's .RowFilterwith the parameter you enter in the textbox. Then bind that DataView to the RowSourceof GridView. In the above sample it's using:
需要注意的重要一点是,您可以将DataView's.RowFilter与您在textbox. 然后结合该数据视图到RowSource的GridView。在上面的示例中,它使用:
dataview.RowFilter = "Year(Duedate) = " + textBox1.Text + ";
dataGridView1.DataSource = dataview;
You may also find lots of other articles as well here in SO.
您还可以在 SO 中找到许多其他文章。
回答by bonCodigo
your select statement for filtering remarks:
您用于过滤评论的选择语句:
SELECT caseno, duedate, remarks FROM yourTable name where remarks = '"& txt1.text &"'
you can also make a combobox that consist of list of remarks and replace txt1.text to combobox1.text
您还可以制作一个包含备注列表的组合框,并将 txt1.text 替换为 combobox1.text
to put it on a datagridview: (in your button event)
把它放在一个 datagridview 上:(在你的按钮事件中)
'declare your dataset and adapter
Dim adapter As MySqlDataAdapter
Dim ds As New DataSet
Try
connect()
adapter = New MySqlDataAdapter("SELECT caseno, duedate, remarks FROM yourTable name where remarks = '"& txt1.text &"'", con)
adapter.Fill(ds)
yourdatagridview.DataSource = ds.Tables(0)
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
回答by spajce
How about BindingSource.Filter
BindingSource.Filter = "caseno Like '" textBox.Text + "%'"
回答by Nadeem Shaik
by using dataview row filter you can filter the data
通过使用数据视图行过滤器,您可以过滤数据

