Sorting DataGridView by Dates

Solved
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:

01/01/2001 00:00
20/10/2015 00:00
30/05/2014 00:00


Thank you for your help.

2 answers

cs_Le Pivert Posted messages 8437 Status Contributor 730
 
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
0
Anonyme209 Posted messages 761 Status Member 19
 
Hello,
I tried:
DataGridView1.Columns(0).ValueType = GetType(Date)


but it's still not working better.
0
cs_Le Pivert Posted messages 8437 Status Contributor 730
 
You can use a comparator like for a listView.

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
0
Anonyme209 Posted messages 761 Status Member 19
 
Hello,

The code works but disrupts the functionality of my program.
Is there a component (even if not directly integrated) that automatically sorts in chronological order?
I would just need to put my lines into this component and then return them to my datagridview in the correct order.
0
cs_Le Pivert Posted messages 8437 Status Contributor 730 > Anonyme209 Posted messages 761 Status Member
 
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:

'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
0
Anonyme209 Posted messages 761 Status Member 19
 
I managed to find and solve the compatibility issue with your first code, and it now works perfectly.
Thank you very much for your help.
0