Solved

DataGridView sorting numeric columns

Posted on 2006-06-20
2
700 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now