Solved

DataGridView sorting numeric columns

Posted on 2006-06-20
2
709 Views
Last Modified: 2008-01-09
Dear Expert,

I'm filling a DataGridView the following way:


               NewDataGridViewRow = New DataGridViewRow
               NewDataGridViewRow.CreateCells(DataGridView1)
               '
               NewDataGridViewRow.Cells(dgv.Opdracht).Value = recOrders("bestelnummer").Value.ToString
               NewDataGridViewRow.Cells(dgv.Klant).Value = recOrders("debiteur").Value.ToString
               NewDataGridViewRow.Cells(dgv.Klantnaam).Value = recOrders("bezorgnaam").Value.ToString

(I could also use binding with a dataset, but I am not allowed to do this.)

Column "Klant" is of type DataGridViewTextBoxColumn, but contains actually numeric data.

Now I want to sort on "Klant" with:

DataGridView1.Sort(DataGridView1.Columns("Klant"), System.ComponentModel.ListSortDirection.Ascending)

Now I get a result like this for the column "Klant":

10
300
325
77
77
77
77
9

it should be:

9
10
77
77
77
77
300
325


So sorting is not in a numeric way,but character way

QUESTION:

Can I use something else than "DataGridViewTextBoxColumn" that treats the data numeric?

If I have to use DataGridViewTextBoxColumn can (easily) I sort in a numeric way?

Thanks,

dgb
0
Comment
Question by:dgb
[X]
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
2 Comments
 
LVL 4

Accepted Solution

by:
davehunt00 earned 500 total points
ID: 16944712
what happens if you change this:

recOrders("debiteur").Value.ToString

to this:

recOrders("debiteur").Value
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16944926
You need to implement a numeric comparer:

Public Class NumericComparer
    Implements IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        If TypeOf x Is String And TypeOf y Is String Then
            Return String.Compare(Val(x), Val(y))
        ElseIf TypeOf x Is Integer And TypeOf y Is Integer Then
            Return Compare(x, y)
        End If
    End Function

End Class

And, then implement:

   DataGridView1.Sort(New NumericComparer)

Bob
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 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