How To Re-Sort A DataTable in VB.Net
The .Net framework can do the sorting for us if we want to re-sort a DataTable by one of the DataColumns in it. Here's how to re-sort the data in a DataTable using the DefaultView object.
Firstly, it's worth mentioning that using this method doesn't actually sort the DataRows in the underlying DataTable, rather it gives us a DataView which we can use to read the DataRows in the order we specify. Using the DataView in this way is extremely fast.
Dim dt As DataTable Dim dr As DataRow ' ' Read the data into your DataTable here ' ' Specify which field to sort the DataView by dt.DefaultView.Sort = "Field1" ' Loop through the sorted DataView, outputting the data as we go For Each drv As DataRowView In dt.DefaultView dr = drv.Row Debug.WriteLine(dr("Field1") & vbTab & dr("Field2")) Next
Here, we're outputting all the data in order of field Field1. You can also specify more than one field for the Sort property, like dt.DefaultView.Sort = "Field1, Field2". You could also bind a grid or another control to the DefaultView object after applying the sort.
Simple as that. Hope that's what you were looking for.