Sorting DataGridView by Dates
Solved
Anonyme209
Posted messages
761
Status
Member
-
Anonyme209 Posted messages 761 Status Member -
Anonyme209 Posted messages 761 Status Member -
Hello,
In the column of my DataGridView, I have various dates.
I would like my DataGridView to sort my data by dates in chronological order and vice versa.
With DataGridView.Sort, the dates are sorted like normal text, and it looks like this:
Thank you for your help.
In the column of my DataGridView, I have various dates.
I would like my DataGridView to sort my data by dates in chronological order and vice versa.
With DataGridView.Sort, the dates are sorted like normal text, and it looks like this:
01/01/2001 00:00
20/10/2015 00:00
30/05/2014 00:00
Thank you for your help.
2 answers
Hello,
You need to specify Date Type and not String in your column.
See this:
https://www.developpez.net/forums/d897729/dotnet/langages/vb-net/trier-colonne-type-date-datagridview/
--
@+ Le Pivert
You need to specify Date Type and not String in your column.
See this:
https://www.developpez.net/forums/d897729/dotnet/langages/vb-net/trier-colonne-type-date-datagridview/
--
@+ Le Pivert
You can use a comparator like for a listView.
Here is the code:
--
@+ The Woodpecker
Here is the code:
Private Sub DataGridView1_SortCompare(sender As Object, e As DataGridViewSortCompareEventArgs) Handles DataGridView1.SortCompare 'Retrieve the component Dim grid As DataGridView = DirectCast(sender, DataGridView) 'Define the column names (to change according to your own settings) Dim colonneDate As String = "Date" 'to adapt 'Processing depending on the column where the currently edited cell is located Select Case e.Column.Name Case colonneDate e.SortResult = System.DateTime.Compare(CDate((e.CellValue1)), CDate((e.CellValue2))) e.Handled = True End Select End Sub 'this code can be placed in a button Private Sub DataGridView1_ColumnHeaderMouseDoubleClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseDoubleClick With DataGridView1 .Sort(.Columns("Date"), System.ComponentModel.ListSortDirection.Descending) End With End Sub 'this code can be placed in a button Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick With DataGridView1 DataGridView1.Sort(.Columns("Date"), System.ComponentModel.ListSortDirection.Ascending) End With End Sub --
@+ The Woodpecker
Dans l'exemple que je t'ai montré, il n'y a qu'une colonne, il faut mettre toutes les colonnes et voir si cela n'altère pas ton programme:
Correct:
Dim colonneDate As String = "Date" 'to adapt
instead of
Dim colonneDate As String = "Date" 'to adapt
Otherwise last option, put the dates in American format like for the photos:
2015/02/15
'Define the column names (to be changed according to your own parameters) Dim colonneDate As String = "Date" 'to adapt Dim colonneNom As String= "Nom" 'to adapt 'etc. 'Processing based on the column where the currently edited cell is located Select Case e.Column.Name Case colonneDate e.SortResult = System.DateTime.Compare(CDate((e.CellValue1)), CDate((e.CellValue2))) e.Handled = True Case colonneNom e.SortResult = System.DateTime.Compare(String((e.CellValue1)), String((e.CellValue2))) e.Handled = True . End Select 'etc.
Correct:
Dim colonneDate As String = "Date" 'to adapt
instead of
Dim colonneDate As String = "Date" 'to adapt
Otherwise last option, put the dates in American format like for the photos:
2015/02/15
I tried:
but it's still not working better.