Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Sort column in DataGridView numerically in unbound mode

Posted on 2006-07-12
Last Modified: 2008-02-26
I have added a column to an unbound DataGridView in VS2005 and need to have the column sort "numerically". I have used this line of code:
DataGridViewItems.Columns(0).ValueType = System.Type.GetType("System.Int32")
which does indeed seem to change the property, but it still sorts the columns "alphabetically". How can I accomplish this?

Bruce Mutch
Question by:bmutch
  • 3
  • 2
LVL 10

Accepted Solution

vadim63 earned 95 total points
ID: 17096151
Why "DataGridViewItems"? This code works perfectly:

Me.DataGridView1.Columns(0).ValueType = System.Type.GetType("System.Int32")
LVL 10

Expert Comment

ID: 17096160
And another thing. You have to set the ValueType before you put any data in.

Author Comment

ID: 17096434
that is the same as the code I am using, and I am setting it before I put any data in, and it is still not sorting correctly ( numerically), (i.e.) 9999 is appearing before 27895 when I sort descending.

This column is a "link" column if that makes any difference.

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 10

Expert Comment

ID: 17096492
Sorry, no idea. The code, I posted shows me correct sorting. i.e.

Assisted Solution

kaliyugkaarjun earned 30 total points
ID: 17097718

This link might help u


Use int instead of String in the following expression .

                   this.ViewState["_orderBy"].ToString()+" ASC";

Author Comment

ID: 17103621
ok found the problem,
for some crazy reason I had just copied over this code and did not change it:

row.CreateCells(DataGridViewItems, .Item("OrderNum").ToString().Trim() ...

of course adding the ToString... messed it all up.

Thanks to both. Since vadim63 had more respnses I'll give him the majority of the points.


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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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