How to sort a String column as Numeric?

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default How to sort a String column as Numeric?

    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.DataGridSortCommandEvent Args) Handles
    dgResults.SortCommand
    Dim dv2 As New DataView
    dv2 = Session("ds")
    dv2.Sort = e.SortExpression
    dgResults.DataSource = dv2
    dgResults.DataBind()
    End Sub
    Todd Guest

  2. Similar Questions and Discussions

    1. numeric part of a string
      Can anyone tell me how can I get the numeric part of a string . for example I'm using this..Select substring('walnut 2224...
    2. string becomes numeric?
      Hi, I am trying to dump a query out into an excel. I found that a varchar datatype column will be converted to numeric in the Excel. For...
    3. Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB.
      Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. hi, guys i have asp...
    4. Descending numeric sort using Guttman-Rosler Transform
      The following program is a simple example of sorting positive integers using the Guttman-Rosler transform: print map { substr($_, 4) } sort map {...
    5. Problem converting array string back to numeric
      I'm afraid I don't know enough about your situation to help. How are you sending your values to ASP? It sounds to me like this is more like an ASP...
  3. #2

    Default Re: How to sort a String column as Numeric?

    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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139