How to filter a dataview in Visual Basic.net?
Apply the filter after creating the dataview
If you have a Datatable in a Dataset, you sometimes want to view only a selection of the table. You can accomplish this by using a dataview.
Suppose you want to view all rows from table "SomeTable", having id smaller than 25. Suppose you have a dataset called MyDataSet. Filter the rows of the datatable as follows:
Dim MyDataview as DataView
MyDataview = New DataView(MyDataSet.Tables("SomeTable"))
MyDataview.RowFilter = "id < 25"
Apply the filter during initialisation of the dataview
Another option is to apply the filter during the initialisation of the dataview.The example above can be rewritten as:
Dim MyDataview as DataView
MyDataView = New DataView("SomeTable", "id < 25", "id", DataViewRowState.CurrentRows)
or even in one line:
Dim MyDataview as DataView = New DataView("SomeTable", "id < 25", "id", DataViewRowState.CurrentRows)
The third parameter is the sort order for the dataview.
Choosing the right method
Both methods can be better or worse, depending on the purpose of the dataview. If you want to reuse the dataview, and applying dynamically a filter during runtime multiple times, you can better use the first method. If you use the dataview only with one filter, the second method has the advantage of minimizing memory and speed.
|