How to sort a String column as Numeric?

T

Todd

I have a datagrid (dgResults) that I am populating from a SQL stored
procedure. After I fill my dataset (ds), I cache the dataset this
way: Session("ds") = ds.Tables("Table1").Defaultview. After this I
set the Grid's datasource to the dataset and bind the Grid.

My sort is working fine for all but 2 columns. The 2 giving me
trouble are formatted as Currency. It's sorting these as if they are
strings ($1, $11, $2, $22, etc). How can I get these to sort in the
proper order (numeric)? I don't know if I need to change anything in
the sort command procedure, but here it is below:

Private Sub dgResults_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
dgResults.SortCommand
Dim dv2 As New DataView
dv2 = Session("ds")
dv2.Sort = e.SortExpression
dgResults.DataSource = dv2
dgResults.DataBind()
End Sub
 
T

Todd

in the sort_command sub I thought that converting all the string typed
numbers back to decimals, then sorting them, then re-binding them to
the DataGrid would work. But it still results in the numbers being
sorted as strings:

997.48
974.63
9389.42
927.43

The code I used in the sort_command sub is below:

Dim y As Integer
Dim x As Integer = 0
Dim dv3 As New DataView
dv3 = Session("ds") 'the dataset is cached
y = dv3.Count
While x < y
'convert each item in the Dataview back to type Decimal
dv3.Item(x).Row.Item("CustBenefit") =
CDec(dv3.Item(x).Row.Item("CustBenefit"))
x += 1
End While
'sort all the decimals
dv3.Sort = e.SortExpression
'set the datasource and re-bind it
dgResults.DataSource = dv3
dgResults.DataBind()

Can anyone please help with this issue? It is very annoying that I
have not been able to find a resource to help answer such a silly
problem. I'm only trying to sort dollar amounts, not discover the
meaning of life! Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top