Solved

How can I get Text values that appear as numbers sort in 'numeric' order in a datagrid?

Posted on 2007-03-23
6
232 Views
Last Modified: 2010-04-23
I have database columns that are defined as Text. One, labeled PartID sometimes has values such as 1,2,3...etc. The problem is when I want to sort in a d dataGrid, it will  sort like 1,11,12,2,3,32,4...etc. How can I get it to sort as if they were consecutive numbers? The fielf is defined Text because more often than not, the value may be alpha in nature too. Thank you in advance for code examples.
0
Comment
Question by:IT_Steve
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 18784692
use this

select convert(int, partId) from demo order by convert(int,partid) asc
0
 

Author Comment

by:IT_Steve
ID: 18788276
I apologize for forgetting to include I am using Access for my database.  I have not found a way to make your suggestion work. Is that for stored procedures? I will need a solution that can deal with values such as 1,2,3..,Dave's-40,SJ,... etc.
0
 
LVL 27

Accepted Solution

by:
planocz earned 500 total points
ID: 18789802
The only way to get around it is to format that field to have a leading zero if the number is between 1 and 9.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 18790803
use it like this

    Dim ds As New DataSet
    Dim da As SqlDataAdapter
    Dim sql As String
    Dim myConnection As SqlConnection
    Dim ConnStr As String = "ur connection string here"

            myConnection = New SqlConnection(ConnStr)

            sql = "select convert(int, partId), partname,partdesc,allothercolumns from demo order by convert(int,partid) asc"
            da = New SqlDataAdapter(sql, myConnection)
            da.Fill(ds, "demo")

0
 

Author Comment

by:IT_Steve
ID: 18792716
Ajaysharmaapjs -
When I try the code you suggest, I get the error: Undefined function 'Convert' in expression. I am using VB.NET 2003. Perhaps Convert is a built in method in 2005? But I was wondering how it willl behave if the partID was 'Steve'sWidget4'? Remember, some partID look numeric, 1,72,4, but many are mixed like 'Steve'sWidget4' or all alpha chars 'NinethParcel'.

Planoz -
I was hoping to avoid the 0 but may be resigned to that. I will wait awhile to see if there are other suggestions.
0
 

Author Comment

by:IT_Steve
ID: 18800070
Planoz - Your solution answered my question as asked and works. I was intrigued by Ajaysharmaapjs 's Select CONVERT(... concept but it didn't work for me.  Thank you for your ideas.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question