Visual Basic: Sort a Dataset's DataTable object

Platform:Visual Basic
Task:Sort a Dataset's DataTable object
Discussion:
This should be easier than it is. A datatable object doesn't have a sort method. That would be too easy and logical. In order to complicate this simple task you need to build a DataView object first. An alternative is to sort the table's binding source (see second example)
Example:
'DataTable1 is the DataTable you want to sort.
'Get the DefaultViewManager of a DataTable.
Dim view As DataView = DataTable1.DefaultView

'By default, the first column sorted ascending.
view.Sort = "State, ZipCode DESC"

'or you could sort the table's binding source
Me.DataTable1BindingSource.Sort = "State, ZipCode DESC"

'this worked for me once also
SheepMonitoringDataSet.tblSurveys.DefaultView.Sort = "Date,Location"
For Each mRow As DataRow In SheepMonitoringDataSet.tblSurveys.DefaultView.ToTable.Rows
   'do something with these sorted records
Next
Back to Index