Locations of visitors to this page


Custom Sort of DataGridView

Onteora Software

Ken Tucker's Blog

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Custom Sort of DataGridView

Custom Sort of DataGridView



Someone asked me about doing a custom sort on a datagridview during my datagridview session at code camp. Unfortunately this will not work if the datagridview is bound to a datasource. This example numbers as string data to a datagridview. When you sort this column the values will not be in numeric order. This example converts the string to a number and sorts based on the number.




Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.ColumnCount = 1
        DataGridView1.Columns(0).HeaderText = "String"

        For x As Integer = 0 To 100
            DataGridView1.Rows.Add(New String() {x.ToString})
        Next
    End Sub

    Private Sub DataGridView1_SortCompare(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewSortCompareEventArgs) Handles DataGridView1.SortCompare
        Dim intValue1, intValue2 As Integer
        If Not Integer.TryParse(e.CellValue1.ToString, intValue1) Then Return
        If Not Integer.TryParse(e.CellValue2.ToString, intValue2) Then Return
        If intValue1 = intValue2 Then
            e.SortResult = 0
        ElseIf intValue2 > intValue1 Then
            e.SortResult = -1
        Else
            e.SortResult = 1
        End If
        e.Handled = True
    End Sub
End Class

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: DataGridView
Posted by Ken Tucker on Monday, February 06, 2006 11:12 PM
Permalink | Comments (2) | Post RSSRSS comment feed

Related posts

Comments

Philip gb

Tuesday, July 15, 2008 11:25 AM

Philip

Ken - not sure if you still support this thread. I have been looking around for support to sort an un-bound datagridview, and this seems the only place (since 2006!)?

QN. I don't understand the logic of the code (I am a newbie cum enthusiast); What column does this code sort? I am going to assume its the first row and give it a run.

Also, is it possible to have a snippet of sorting dates in the first column of an unbound datagridview with a total of 5 columns?

Thanks - Philip

Philip gb

Sunday, October 12, 2008 3:54 AM

Philip

I actually managed to sort this out, and this is how I did it for an un-bound datagridview in a win forms project VB2008Express.

Private Sub DataGridView1_SortCompare(ByVal sender As Object, ByVal e As DataGridViewSortCompareEventArgs) Handles DataGridView1.SortCompare

If e.Column.Index = 0 Then
Dim DateValue1, DateValue2 As Date
If Not Date.TryParse(e.CellValue1.ToString, DateValue1) Then Return
If Not Date.TryParse(e.CellValue2.ToString, DateValue2) Then Return
If DateValue1 = DateValue2 Then
e.SortResult = 0
ElseIf DateValue2 > DateValue1 Then
e.SortResult = -1
Else
e.SortResult = 1
End If
End If
e.Handled = True
End Sub

I then call the routine from within one of my subs that loads data like so:

'sort routine before loading comboboxes
Dim newcolumn As DataGridViewColumn = DataGridView1.Columns(0)
Dim direction As ListSortDirection
direction = ListSortDirection.Ascending
DataGridView1.Sort(newcolumn, direction)

I am posting it here in the hope that like me, if some-one needs it as desperately as I did, they can have access to it. Also, if this code is not as efficient as can be, I am hoping someone can refine it for our good use!
thanks again for providing the forum and the spark!

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Wednesday, November 19, 2008 12:50 PM