sorting number in ascending way but still mixes the numbers

Discussion in 'ASP .Net' started by Richard, Aug 3, 2004.

  1. Richard

    Richard Guest

    When i try sorting in the database, it sorts the numbers:
    0
    1
    102
    2
    304
    305
    4
    etc....

    but i want to have it sorted like:
    0
    1
    2
    4
    102
    304
    305

    What must i do to make it so?
    I just use a normal datagrid with sorting capabilities enabeld.

    thx in advance

    Richard
    Richard, Aug 3, 2004
    #1
    1. Advertising

  2. Richard

    Curt_C [MVP] Guest

    Are they coming through as a numeric datatype or as strings?


    --
    Curt Christianson
    Owner/Lead Developer, DF-Software
    Site: http://www.Darkfalz.com
    Blog: http://blog.Darkfalz.com


    "Richard" <> wrote in message
    news:uLmyX%...
    > When i try sorting in the database, it sorts the numbers:
    > 0
    > 1
    > 102
    > 2
    > 304
    > 305
    > 4
    > etc....
    >
    > but i want to have it sorted like:
    > 0
    > 1
    > 2
    > 4
    > 102
    > 304
    > 305
    >
    > What must i do to make it so?
    > I just use a normal datagrid with sorting capabilities enabeld.
    >
    > thx in advance
    >
    > Richard
    >
    >
    Curt_C [MVP], Aug 3, 2004
    #2
    1. Advertising

  3. Hi Richard,

    As for the sorting problem you mentioned, I also agree with Curt_C that the
    problem is likely due to the DataTable's Column type. How do you add the
    DataColumn into DataTable? The DataColumn has several constructors which
    mapping to the DataTable.Columns.Add methods such as

    Add(string)
    Add(string, Type)
    ......

    #DataColumn Constructor
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
    frlrfsystemdatadatacolumnclassctortopic.asp

    and if you add the column as
    DataTable.Columns.Add("column name");
    then, the column is added as a stirng type column and when being sorting,
    the column's data will be sort as ASCII index sorting.
    So if you want to have a int or float type column being sort by number, you
    need to add this column and specify the column type explicitly, such as

    DataTable.Columns.Add("int column", typeof(int));
    DataTable.Columns.Add("float column",typeof(float));

    Please have a check. Hope helps. Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    Steven Cheng[MSFT], Aug 4, 2004
    #3
  4. Richard

    Richard Guest

    I dont know how to implement this,

    my code for the datagrid is,
    ----
    <asp:DataGrid runat="server" id="myDataGrid" autogeneratecolumns="false"
    Border="0" ShowHeader="True"
    AllowPaging="True" HorizontalAlign="center" PageSize="50"
    PagerStyle-Mode="NumericPages" PagerStyle-PageButtonCount="100"
    PagerStyle-HorizontalAlign="center" DataKeyField="U_ID"
    AllowSorting="True" OnSortCommand="myDataGridMain_Sorting"
    OnItemDataBound="myDataGrid_ItemDataBound" width="100%"
    OnPageIndexChanged="myDataGrid_PageIndexChanged">
    <columns>
    <asp:BoundColumn DataField="A_LEVELNAAM" SortExpression="A_LEVELNAAM"
    HeaderText="<strong>Level</strong>" />
    <asp:BoundColumn DataField="U_Posts" SortExpression="U_Posts"
    HeaderText="<strong>Posts</strong>" />
    <asp:BoundColumn DataField="U_Registered_On"
    SortExpression="U_Registered_On" HeaderText="<strong>Join Date</strong>"
    DataFormatString="{0:dd-M-yyyy}" />
    <asp:BoundColumn DataField="U_Last_Logged_In"
    SortExpression="U_Last_Logged_In" HeaderText="<strong>Last Active</strong>"
    DataFormatString="{0:dd-M-yyyy}" />
    </columns>
    </asp:DataGrid>
    ----
    The field i want to sort this way is the U_Posts. I use that field for
    whenever a person posts a item on the website, it does +1. When i look at
    the users information i just want to see what user made the most posts on
    the website etc.

    Do you have a way how i can implement your idea into this?

    my code behind for the databind()
    ----------------------
    Sub BindData(ByVal sortExpr As String)
    Dim ds As New DataSet
    Dim dsc As SqlDataAdapter
    Dim strSQL As String
    Dim strCon As String

    'strSQL = "SELECT * FROM tbl_Users ORDER BY " & sortExpr & ""
    strSQL = "SELECT tbl_Users.*, tbl_Level.A_LEVELID,
    tbl_Level.A_LEVELNAAM FROM tbl_Users INNER JOIN tbl_Level ON
    tbl_Users.U_ToegangsID = tbl_Level.A_LEVELID ORDER BY " & sortExpr & ""
    strCon = ConfigurationSettings.AppSettings("DigifanDatabaseCon")

    dsc = New SqlDataAdapter(strSQL, strCon)
    dsc.Fill(ds, "tbl_Users")

    myDataGrid.DataSource = ds.Tables("tbl_Users").DefaultView
    myDataGrid.DataBind()
    End Sub
    --------------------


    Thx in advance


    ----


    "Steven Cheng[MSFT]" <> schreef in bericht
    news:...
    > Hi Richard,
    >
    > As for the sorting problem you mentioned, I also agree with Curt_C that

    the
    > problem is likely due to the DataTable's Column type. How do you add the
    > DataColumn into DataTable? The DataColumn has several constructors which
    > mapping to the DataTable.Columns.Add methods such as
    >
    > Add(string)
    > Add(string, Type)
    > .....
    >
    > #DataColumn Constructor
    >

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
    > frlrfsystemdatadatacolumnclassctortopic.asp
    >
    > and if you add the column as
    > DataTable.Columns.Add("column name");
    > then, the column is added as a stirng type column and when being sorting,
    > the column's data will be sort as ASCII index sorting.
    > So if you want to have a int or float type column being sort by number,

    you
    > need to add this column and specify the column type explicitly, such as
    >
    > DataTable.Columns.Add("int column", typeof(int));
    > DataTable.Columns.Add("float column",typeof(float));
    >
    > Please have a check. Hope helps. Thanks.
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    > Get Preview at ASP.NET whidbey
    > http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    >
    >
    Richard, Aug 4, 2004
    #4
  5. Hi Richard,

    Thanks for your response and the detailed code you provided. Since you use
    DataAdapter to fill dataset. So the DataColumns's type is auto set by
    Adapter when filling the datas into dataTable in the DataSet. I think you
    can add some additional code ( or use F5 debug ) to have a check on the
    DataTable's colum
    s DataType( for the "U_Posts" field). If the columns's DataType is string
    rather than numeric type, when sorting, the data will be sorted by ascii
    rather than numeric value. So please have a check on it.
    (You can add the checking code after the dataadapter fill in the dataset
    and before bind the dataset to the datagrid. )
    In addition, what's the "U_Posts" column's type in the DataBase?


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    Steven Cheng[MSFT], Aug 5, 2004
    #5
    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. Seansan
    Replies:
    1
    Views:
    592
    Joe Smith
    Jan 30, 2005
  2. Alex
    Replies:
    0
    Views:
    454
  3. Werner Butscher
    Replies:
    2
    Views:
    1,240
    Werner Butscher
    Aug 2, 2004
  4. dwelden
    Replies:
    11
    Views:
    480
    Neil Cerutti
    Jan 4, 2007
  5. Replies:
    0
    Views:
    300
Loading...

Share This Page