How to sort a String column as Numeric?

Discussion in 'ASP .Net Datagrid Control' started by Todd, Nov 24, 2003.

  1. Todd

    Todd Guest

    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
     
    Todd, Nov 24, 2003
    #1
    1. Advertising

  2. Todd

    Todd Guest

    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.
     
    Todd, Nov 25, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    978
    X-Centric
    Jun 30, 2005
  2. darrel
    Replies:
    4
    Views:
    864
    darrel
    Jul 19, 2007
  3. Navin
    Replies:
    1
    Views:
    762
    Ken Schaefer
    Sep 9, 2003
  4. filippo

    numeric sort on string like a123-3

    filippo, Jul 18, 2006, in forum: Perl Misc
    Replies:
    18
    Views:
    187
    Ted Zlatanov
    Jul 24, 2006
  5. Rick
    Replies:
    0
    Views:
    171
Loading...

Share This Page