Click here to Skip to main content
15,896,500 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have read numerous on-line articles on sorting a date column in DataGridView, however they are nearly all tied to a database of one kind or another. A few of them say if you make sure the data is of type DateTime the column will sort by clicking on the glyph of the column header. This is not true. I have converted my data to DateTime type and verified that it is by using the IsDate() method. It still sorts as if the data were strings or numbers. My solution, probably not a good one, is to add a column for sorting with dates converted to numeric values using ToFileTime(). This works because the ToFileTime() calculates the time from Jan 1 1601 to the date you enter and returns the numeric value which of course sorts properly.

I have read many of the MSDN articles or tutorials on sorting columns in DGV and not one of them that I can find sort dates, it is always strings or numbers.

Can an unbound DataGridView column really sort a DateTime filled column...I don't think so. But, if it can, how?
Posted
Comments
Maciej Los 14-Oct-14 2:11am    
WinForms? WebControls? WPF? What kind of DGV?
Member 10628309 15-Oct-14 1:26am    
WinForms
Maciej Los 15-Oct-14 1:47am    
What's the format of date?
Please, provide the code you're trying to sort date-time data. Use "Improve question" widget.
Member 10628309 15-Oct-14 16:31pm    
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
h = 0
j = 0
myColString = ""
For Me.g = 0 To (dgvEds.RowCount * 6) - 1
myString = dgvEds(h, j).Value
If h = 5 Then
Try
Dim dteDate As String = dgvEds(5, j).Value.ToString()
Dim dteDOB As DateTime = DateTime.Parse(dteDate)
dgvEds(5, j).Value = Format(dteDOB, "MM/dd/yyyy")
Catch
End Try
End If
myColString = myColString & myString & "|"
h = h + 1
If h > 5 Then
j = j + 1
h = 0
End If
Next
My.Settings.saveData = myColString
End Sub
Maciej Los 15-Oct-14 16:46pm    
Use "Improve question" widget to upgrade your question.

1 solution

When entering text directly into a DGV column you are entering a string. My solution is to enter the string from the keyboard to the column cell then convert it to date type using Date.Parse and then send it back to the same column cell and redisplay it using DGV.Refresh(). The Test Project uses one button with the Tab Stop property set to False. The DGV column format property is set to MM/dd/yyyy. The AllowUserToAddRows property is set to False. The button adds a row making the DGV cell available to enter date type text. By pressing the Enter key the conversion, formatting and redisplay takes place. Now clicking the glyph at the top of the column sorts dates properly, either ascending or descending. The Test Project accepts many types of date formats, e.g. year/month/day, month-day-year, month spelled out like July etc. It converts all of the different formats to MM/dd/yyyy.

Here is the code:

DGV Date Column Sort

Public Class Form1
Private i As Integer

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
dgvData.Rows.Add()
SendKeys.Send(vbTab)
SendKeys.Send(vbTab)
End Sub

Private Sub dgvData_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgvData.CellEndEdit
Try
Dim myDate As Date = dgvData(0, i).Value
dgvData(0, i).Value = Date.Parse(myDate)
dgvData.Refresh()
i = i + 1
Catch ex As Exception
MsgBox("You must enter a valid date")
Return
End Try
End Sub
End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900