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
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
  • 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.

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

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

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!

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
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.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

696 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