vb.net的dataTable / DataGridView的搜索/排序net、vb、DataGridView、dataTable

2023-09-05 02:35:58 作者:ゎ. 你不懂゜

所以,我有一个DataGridView,并在窗体上的文本框。我希望能够通过在DataGridView搜索和排序比较字符串中的文本框中。例如:   I型ACV,在文本框中包含ACV的所有字符串进行排序顶端。 我完成这一堆涉及datatable.select有的清算和灌装体操,但它的丑陋和缓慢的。什么是最好的做法/正确/正常的方式做到这一点?

So I have a dataGridView and a textbox on a form. I want to be able to search through the dataGridView and sort it compared to the string in the text box. ex: I type "acv" in the text box and all strings containing "acv" are sorted to the top. I'm accomplishing this with a bunch of gymnastics involving datatable.select and some clearing and filling but it's ugly and slow. What would be the best practice/proper/normal way to do this?

推荐答案

使用筛选的数据视图,然后设置你的DataGridView的BindingSource的已筛选数据视图。如果用户清除过滤条件,只需要设置BindingSource的回到原来的默认视图。我建议你​​存储的排序,所以你可以回到原来的数据视图轻松之前的观点。我用这个现在分拣速度快,它的伟大工程。替换列名与你的。你应该能够从原始的DataGridView修改数据视图并应用过滤器,而无需重新结合。只要你的DataGridView绑定到数据视图的开头,然后检索数据视图(如数据源)和修改。我不知道,如果你使用的是BindingNavigator与否。祝你好运。

Use a filtered DataView and then set your DataGridView's BindingSource to the Filtered DataView. If the user clears the filter condition, then just set the BindingSource back to your original default view. I recommend you store the view before sorting so you can go back to the original dataview easily. I use this now for sorting fast and it works great. Replace column names with yours. You should be able to modify the dataview from the original DataGridView and apply the filter without re-binding. Just bind your DataGridView to a DataView at the beginning, then retrieve the DataView (i.e. DataSource) and modify. I'm not sure if you are using a BindingNavigator or not. Good luck.

Dim myDataTable As DataTable = myDataSet.Tables(0)
Dim myDataView As New DataView(myDataTable)

myDataView.RowFilter = "CompanyName LIKE '%" & ddlAlpha.SelectedItem.Text & "%'"
myDataView.Sort = "ContactName"
dataGridView1.DataSource = myDataView
dataGridView1.DataBind()