I have datatable and it has two columns named "RegNo", "Name"
RegNo is a string field.
This is an example

"RegNo"     "Name"
======      =====
    55      name1
    10      name2
    5555    name3
    75      name4
    10A     name5

i used this code for sort

datatable.DefaultView.Sort = "RegNo ASC"

But after sort the RegNo column shows like this.

10
10A
55
5555
75

I want it like this

10
10A
55
75
5555

How to do it.....???

Recommended Answers

All 4 Replies

That's a common sorting problem with numerical data represented as strings. Correct the problem by right-aligning the numbers, embed either spaces or 0s to the beginning of each number so that all numbers are the same width. for example the first value, 10, would become 0010 so that it's the same width as 5555.

Ancient Dragon is right. Make your RegNo field meaningful and unique

Thanks Ancient Dragon and ryanjayson for the reply. It is not be able to do so, (10 change to 0010) because this is real world application. Customers already have this reg nos. I gave an example. Reg no has various patterns. (Eg: 10, 10A, 10-B etc...) Is there no any other way????? :(

Read the data into an array instead of directly into a grid or something else, move the suffix's (A, B, etc) into another column so that the first column is only numeric, sort the array treating the first column as numeric instead of string, then move all data to it's final destination. This also means there is no point in having the datatable sort the data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.