Hi,

I'm trying to sort a DataTable using the following code:

Private Function sortDataTable(ByVal dTable As DataTable) As DataTable
        Dim dView As New DataView(dTable)
        dView.Sort = "Field ASC"

        Return dTable
End Function

But it doesn't work. What is wrong? How can I sort the DataTable?

Thanks,

Ana

Member Avatar

try:

Private Function SortDataTable(ByVal dTable As DataTable, ByVal ColumnName As String, Optional ByVal OrderByAsc As Boolean = True) As DataView
Dim dView As New DataView(dTable)

If OrderByAsc Then
dView.Sort = ColumnName & " ASC"
Else
dView.Sort = ColumnName & " DESC"
End If

Return dView
End Function

Just sort the table directly:
dTable.DefaultView.Sort = ColumnName & " ASC"

commented: Please don't revive 3 year old SOLVED Threads. -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.