admin管理员组文章数量:1321262
I have the following code:
myDataSet.myTable.DefaultView.RowFilter = "Code <> 'X'"
After the line executes, I can see the updated result where no rows contain an X for the Code column, if I hover over DefaultView
. But, when hovering over myDataSet.myTable
the table still contains records where Code = 'X'.
What would I need to do in order to make the myDataSet.myTable
be updated with the results of the filter applied?
I have the following code:
myDataSet.myTable.DefaultView.RowFilter = "Code <> 'X'"
After the line executes, I can see the updated result where no rows contain an X for the Code column, if I hover over DefaultView
. But, when hovering over myDataSet.myTable
the table still contains records where Code = 'X'.
What would I need to do in order to make the myDataSet.myTable
be updated with the results of the filter applied?
1 Answer
Reset to default 1Well, as pointed out, the "view" is in effect another "view" into that data table. Rather similar as to when you use a SQL query against the database. The result of that query is a "view" into the database.
So, you are free to set the "default" view on a data table, and it will filter to that view. And thus you can get BOTH a count of table rows, and a count of filter rows.
And, you can "copy" the results into a new table object if you wish.
This example shows this in action:
Dim dtHotels As DataTable
Dim strSQL As String =
"SELECT * FROM tblHotelsA
WHERE Active = 1
ORDER BY HotelName"
dtHotels = MyRst(strSQL)
dtHotels.DefaultView.RowFilter = "[City] = 'Edmonton'"
Dim MyFilterTable As DataTable = dtHotels.DefaultView.ToTable()
Debug.Print($"dtHotels rows = {dtHotels.Rows.Count}")
Debug.Print($"dtHotels view filter rows = {dtHotels.DefaultView.Count}")
Debug.Print($"Filter table (copy) = {MyFilterTable.Rows.Count}")
Output:
dtHotels rows = 16
dtHotels view filter rows = 8
New Filter table (copy) = 8
So, the view is much in effect like a "query" against that data table, and the row count, and original rows still exist in that data table.
本文标签: vbnetTrouble with Filtering a NET DataTableStack Overflow
版权声明:本文标题:vb.net - Trouble with Filtering a .NET DataTable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742095740a2420538.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
DataTable
itself, other than actually editing each individual row as needed. When you sort and filter, you do it to theDefaultView
. That's why, when you bind aDataTable
, e.g. to aDataGridView
, the data displayed in the UI actually comes from theDefaultView
. Why can't you just get the data from theDefaultView
yourself? If you really must have aDataTable
for some reason, theDefaultView
has aToTable
method, which will create a newDataTable
containing the data exposed by theDefaultView
. – jmcilhinney Commented Jan 17 at 15:35