Solved

DataGridView sorting numeric columns

Posted on 2006-06-20
2
707 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
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

792 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