473,378 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

VB.NET Datagrid Sorting of Numbers

Hi,

I've got a datagrid that has columns containing numbers but when the
user clicks on the column header to sort - the sort is not placing
numbers in the correct order. For example let's say I have the
following column of number data:

1
2
3
6
7
9
10
15
20
25
etc...

After sorting it appears like this:

1
10
15
2
20
25
3
6
7
9

Is there something I can do to rectify this?? Is the datagrid just not
aware of the datatype of the column? I assumed that it would since
it's being loaded from a database but perhaps that is not the case.

Thanks!

Joy

Aug 1 '06 #1
4 10490
Hi,

This is happening because you did not set the type of the data that
your data column would use. By default, the column inputs are
considered as string and that is why they are being sorted that way.

To fix your problem, just add the data type when you create the data
column.

(extract from MSDN)

Private Sub AddDataColumn(ByVal myTable As DataTable)
Dim myColumn As DataColumn
Dim myType As System.Type
myType = System.Type.GetType("System.Int32")
myColumn = New DataColumn("id", myType)
' Set various properties.
With myColumn
.AutoIncrement = True
.AutoIncrementSeed = 1
.AutoIncrementStep = 1
.ReadOnly = True
End With
' Add to Columns collection.
myTable.Columns.Add(myColumn)
End Sub
If you create your DataColumn that way, it would sort properly

Hope that helps

Charlie
si***********@yahoo.com wrote:
Hi,

I've got a datagrid that has columns containing numbers but when the
user clicks on the column header to sort - the sort is not placing
numbers in the correct order. For example let's say I have the
following column of number data:

1
2
3
6
7
9
10
15
20
25
etc...

After sorting it appears like this:

1
10
15
2
20
25
3
6
7
9

Is there something I can do to rectify this?? Is the datagrid just not
aware of the datatype of the column? I assumed that it would since
it's being loaded from a database but perhaps that is not the case.

Thanks!

Joy
Aug 1 '06 #2

ok that makes sense but what if the column can also contain strings?
It happens very rarely but it still happens.

Charlie wrote:
Hi,

This is happening because you did not set the type of the data that
your data column would use. By default, the column inputs are
considered as string and that is why they are being sorted that way.

To fix your problem, just add the data type when you create the data
column.

(extract from MSDN)

Private Sub AddDataColumn(ByVal myTable As DataTable)
Dim myColumn As DataColumn
Dim myType As System.Type
myType = System.Type.GetType("System.Int32")
myColumn = New DataColumn("id", myType)
' Set various properties.
With myColumn
.AutoIncrement = True
.AutoIncrementSeed = 1
.AutoIncrementStep = 1
.ReadOnly = True
End With
' Add to Columns collection.
myTable.Columns.Add(myColumn)
End Sub
If you create your DataColumn that way, it would sort properly

Hope that helps

Charlie
si***********@yahoo.com wrote:
Hi,

I've got a datagrid that has columns containing numbers but when the
user clicks on the column header to sort - the sort is not placing
numbers in the correct order. For example let's say I have the
following column of number data:

1
2
3
6
7
9
10
15
20
25
etc...

After sorting it appears like this:

1
10
15
2
20
25
3
6
7
9

Is there something I can do to rectify this?? Is the datagrid just not
aware of the datatype of the column? I assumed that it would since
it's being loaded from a database but perhaps that is not the case.

Thanks!

Joy
Aug 1 '06 #3
I think, in that case, you would get a "Type Missmatched" error when
you pull data out of the database and try to fill up your datatable.

One thing you could also do is to sort the table yourself by creating a
new table and iterate through the old table and parse it the way you
want it into the new table and disable the sort feature for the
datagrid.

Anyway, personally, I don't think it's a good idea to mix up the data
types.

Regards,

Charlie

Aug 1 '06 #4

I agree but unfortunately I have had no choice in this case - it looks
like I will have to do some special handling then - thanks!

Charlie wrote:
I think, in that case, you would get a "Type Missmatched" error when
you pull data out of the database and try to fill up your datatable.

One thing you could also do is to sort the table yourself by creating a
new table and iterate through the old table and parse it the way you
want it into the new table and disable the sort feature for the
datagrid.

Anyway, personally, I don't think it's a good idea to mix up the data
types.

Regards,

Charlie
Aug 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chris Mayers | last post by:
Hi, I'm trying to sort a DataGrid (dotNet Windows Forms). Trouble is, the column I'm trying to sort on contains part numbers of the form xxx-yyy where xxx - Part Type yyy - Part Size.
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
2
by: DelphiBlue | last post by:
I have a Nested Datagrid that is using a data relations to tie the parent child datagrids together. All is working well with the display but I am having some issues trying to sort the child...
2
by: ChrisM | last post by:
Can anyone please tell me what I'm doing wrong here. I have a Windows Form with a DataGrid on it, and I'm having real problems with the Sorting. It is easy to reproduce the problem I have. If...
2
by: Josema | last post by:
Hi to all, I have a datagrid filled with string values, and when i click in a header column the datagrid is alphanumerical sorted , but i need sort some columns by numeric way... The...
2
by: enak | last post by:
I can not get my datagrid to page. I have a datagrid that I can sort 2 of the columns. This works great. I added paging and when I display the dg it shows 5 pages. (I am showing page numbers at...
2
by: simon | last post by:
I have column of dataType varchar(10). The values in this column are decimal numbers, 'NUL', OR ''. (because of 'NUL' and '' values I haven't defined column as decimal(18,2)) When I fill...
4
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in...
7
beacon
by: beacon | last post by:
I'm writing a program as an assignment that takes 5 sorting algorithms and and tests for the amount of time and the number of comparisons it takes to um, sort an array. I have run into some...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.