0

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

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by lolafuertes
0

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

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.