Sorting date column in datagridview
-
Hello, I am programming in vb.net (VS2005). I am trying to sort date column in datagridview but with no success. When I am clicking on header of the column to sort, I got sorting only by day: for example: 01/05/2009 02/03/2009 03/08/2008 ... instead of getting: 03/08/2008 02/03/2009 01/05/2009 I tried to parse the column to DateTime but it dosn't help. I tried in Cell_Parsing event also to Parse but the evnet didn't fire at all. Can you please give any idea how to do it. Thank you for help
Shay Noy
-
Hello, I am programming in vb.net (VS2005). I am trying to sort date column in datagridview but with no success. When I am clicking on header of the column to sort, I got sorting only by day: for example: 01/05/2009 02/03/2009 03/08/2008 ... instead of getting: 03/08/2008 02/03/2009 01/05/2009 I tried to parse the column to DateTime but it dosn't help. I tried in Cell_Parsing event also to Parse but the evnet didn't fire at all. Can you please give any idea how to do it. Thank you for help
Shay Noy
-
Try setting the
DataGridViewColumn.DefaultCellStyle.Format
as "d". Is the datagridview bound?It's not necessary to be so stupid, either, but people manage it. - Christian Graus
It doesn't help too. The datagridview is Unbound. I had added the columns in design mode through the "Edit Columns" feature of the datagridview. The sortMode is set to Automatic and as you said the DataGridViewColumn.DefaultCellStyle.Format is set to d. Thank you
Shay Noy
-
It doesn't help too. The datagridview is Unbound. I had added the columns in design mode through the "Edit Columns" feature of the datagridview. The sortMode is set to Automatic and as you said the DataGridViewColumn.DefaultCellStyle.Format is set to d. Thank you
Shay Noy
-
You will need to handle the SortCompare event for the datagridview and sort the data there.
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
Thank you, I will try this.
Shay Noy
-
Thank you, I will try this.
Shay Noy
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