May i know how to sort a datatable?My code that show below got any problem?
why the datatable cant sort by qFundCode?
Firstly,my program is add sum datarow where qFundCode show below are to datatable.
qFunCode
WER
WER
WER
THS
THS
THS
AG
AG
AG

After this,i add another datarow where qFundCode show below are to datatable again.
qFundCode
WER
THS
AG

But when i sort the datatable by code show below.

If resultCheckEndOfYear = True Then
            BalanceEndOfYear(dtDTBM)
            dtDTBM = SortData(dtDTBM)
                    Else
            Call BalanceData(dtDTBM)
        End If
Private Function SortData(ByVal dtDTBM As DataTable) As DataTable

        dtDTBM.DefaultView.Sort = String.Format("{0} {1}", "qFundCode", "ASC")
        Return dtDTBM.DefaultView.Table

    End Function
    Private Sub BalanceEndOfYear(ByVal dtDTBM As DataTable)

The result cum out is like tis 1:
qFundCode
WER
WER
WER
THS
THS
THS
AG
AG
AG
WER
THS
AG

The correct result suppose is like below:
WER
WER
WER
WER(the datarow when add by the second times)
THS
THS
THS
THS(the datarow when add by the second times)
AG
AG
AG
AG(the datarow when add by the second times)


Hope sumbody can help me?Thanks

Dani AI

Generated

The observed behavior comes from sorting the DataView but then returning the original table. Setting the view's Sort only changes the order of the DataView; the underlying DataTable keeps rows in insertion order. That is why the second set of rows shows up appended instead of being interleaved with the first set. was right to point at the DataView, but the advice to "must" pass ByRef is misleading — DataTable and DataView are reference types, so modifying their contents is visible even when passed ByVal; ByRef is only needed if you intend to reassign the parameter itself and have that reassignment visible to the caller.

Two practical fixes (choose one):

  • Keep using a DataView end-to-end: sort the DefaultView and have your downstream methods consume the DataView directly. This leaves the sorted order intact and is the simplest change if your processing code can work with a view.

  • Produce a new, sorted DataTable and pass that to methods that expect a DataTable. Example using the view-to-table route:

dtDTBM.DefaultView.Sort = "qFundCode ASC"
Dim dtSorted As DataTable = dtDTBM.DefaultView.ToTable()

If ToTable or CopyToDataTable are not available in your target framework, you can use Select to get a sorted DataRow array and then build a table (guard for zero rows):

Dim rows() As DataRow = dtDTBM.Select("", "qFundCode ASC")
Dim dtSorted As DataTable
If rows.Length > 0 Then
    dtSorted = rows.CopyToDataTable()
Else
    dtSorted = dtDTBM.Clone()
End If

Other tips: do not use DefaultView.Table to get a sorted result (it points back to the original, unsorted table). If you add rows after sorting, reapply the sort or recreate the sorted copy. Also watch for ToTable overloads that can remove duplicates (distinct) and guard CopyToDataTable when there are no rows.

Can you try to use the resulting Dataview instead of the table, because the table is never sorted while the view is:

Dim Dv as Data.DataView
Dv = SortData(dtDTBM)

If resultCheckEndOfYear = True Then
    BalanceEndOfYear(Dv)
Else
    Call BalanceData(Dv)
End If

And change the Sort function as

Private Function SortData(ByVal dtDTBM As DataTable) As DataView
    dtDTBM.DefaultView.Sort = String.Format("{0} {1}", "qFundCode", "ASC")
    Return dtDTBM.DefaultView
End Function

Also you should change the definitions of

Private Sub BalanceEndOfYear(ByRef dtDTBM As DataView)
Private Sub BalanceData(ByRef dtDTBM As DataView)

Specially be carefull when passing parameters. A table or a view 'must' be passed ByRef instead of ByVal in order to work properly.

Hope this helps

Be a part of the DaniWeb community

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