0

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

3
Contributors
2
Replies
35
Views
8 Years
Discussion Span
Last Post by navyjax2
1

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

-1

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

Votes + Comments
Please don't revive 3 year old SOLVED Threads.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.