?
Solved

DataGridView sorting numeric columns

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

777 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