Hi - if you are still searching for an answer, I got one for you. It involves implementing the icomparer class. Add this to the your form:
Public Class DataGridViewSortRowsByDateTime
Implements System.Collections.IComparer
Private Direction As Integer = 1
Public Sub New(ByVal so As SortOrder)
If so = SortOrder.Descending Then
Direction = -1
ElseIf so = SortOrder.Ascending Then
Direction = 1
End If
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer \_
Implements System.Collections.IComparer.Compare
Dim RowOne As DataGridViewRow = CType(x, DataGridViewRow)
Dim RowTwo As DataGridViewRow = CType(y, DataGridViewRow)
Dim Result As Integer = \_
System.DateTime.Compare(RowOne.Cells(0).Value, RowTwo.Cells(0).Value)
Return Result \* Direction
End Function
End Class
NOTE: This sorts on column index(0). You can assign your column index to whatever you like by changing the value of x in the statement:
System.DateTime.Compare(RowOne.Cells(x).Value, RowTwo.Cells(x).Value)
Then after loading your data into the datagridview, add this line at the end:
DataGridView1.Sort(New DataGridViewSortRowsByDateTime(SortOrder.Ascending))
for ascending or:
DataGridView1.Sort(New DataGridViewSortRowsByDateTime(SortOrder.Descending))
I hope that helps (it did for me!)
modified on Friday, November 20, 2009 3:50 AM