1

I have DataSet Named as Album which contain four DataTables.Name and Order of those tables are

  1. Flower
  2. Error
  3. Photoz
  4. Apple

I need to arrange all these DataTables in Dataset(Album) as alphabetical order. Like as follows

  1. Apple
  2. Error
  3. Flower
  4. Photoz

I have searched in google but there is no even reltive solution

  • Kindly refer: http://stackoverflow.com/questions/670701/how-to-sort-dataset – Aki Jun 26 '14 at 10:52
  • @Aki No that's for Sorting columns inside the data table but what i want is sort list of datatbles in Dataset –  Jun 26 '14 at 10:54

1 Answers1

3

Try this, it will take a dataset you pass to it and return a new dataset with the tables ordered appropriately by name.

Imports System.Collections.Generic

Function OrderDatatables(ByVal ds As DataSet) As DataSet

    Dim tableList As New SortedList(Of String, DataTable)
    Dim sortedDataset As New DataSet

    For Each table As DataTable In ds.Tables
        tableList.Add(table.TableName, table)
    Next

    For Each tableItem As KeyValuePair(Of String, DataTable) In tableList
        sortedDataset.Tables.Add(tableItem.Value.Clone())
    Next

    Return SortedDataset

End Function

Update

For adding DataTable Values

Function OrderDatatables(ByVal ds As DataSet) As DataSet

    Dim tableList As New SortedList(Of String, DataTable)
    Dim sortedDataset As New DataSet

    For Each table As DataTable In ds.Tables
        tableList.Add(table.TableName, table)
    Next

    For Each tableItem As KeyValuePair(Of String, DataTable) In tableList
        sortedDataset.Tables.Add(ds.Tables(tableItem.Value.Clone().ToString()).Copy())
    Next

    Return SortedDataset

End Function
Dimitri
  • 1,188
  • 5
  • 7
  • 1
    Hi It's working as expected but it Truncate the DataTable Values. I need datatable value should not remove... –  Jun 27 '14 at 03:50