Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DataGridView sorting numeric columns

Posted on 2006-06-20
2
Medium Priority
?
732 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 2000 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

Technology Partners: 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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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 …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

618 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