?
Solved

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

Posted on 2007-03-23
6
Medium Priority
?
239 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 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

850 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