cobolinx1
asked on
Datagridview sorting by date
Currently I have a datagridview that I created the columns at run time and added data to it row by row. (No databinding) How do I make a column that has dates (01/01/2009) sortable by the date? It appears to interpret it as a string is there a way to say this is a date column treat it like a date not a string. I tried creating a cell template but it doesn't work. Do I have to make the data into YYYY-MM-DD format somehow when the user clicks the header and then switch it back?
Dim txtCelldate As DataGridViewCell = New DataGridViewTextBoxCell
With txtCelldate
'Put specifics about the cell here, such as backcolor, etc.
.Style.Format = "DD/MM/YYYY"
End With
Dim txtCelldate As DataGridViewCell = New DataGridViewTextBoxCell
With txtCelldate
'Put specifics about the cell here, such as backcolor, etc.
.Style.Format = "DD/MM/YYYY"
End With
the 3rd column is type of DateTime and it expects DateTime objects...
The other alternative is to use something like a DataTable, and set it as the DataSource of the DataGridView. But either way, the column that has the Date in it needs to be of a Date data type, like sedqwick explained.
The benefit of using a DataTable is that it is very easy to work with, is sortable, and can be filtered very easy, which you won't be able to do just using the DataGridView.
The benefit of using a DataTable is that it is very easy to work with, is sortable, and can be filtered very easy, which you won't be able to do just using the DataGridView.
ASKER
I still treats the dates as strings when I sort them.
ASKER
The problem with setting the datasource is that I have data that I need to convert before displaying it and there are some combobox fields that I have a datasource already associated with it so If the user clicks on the field there are values that may not be in the dataset that I would bind everything to.
from msdn:
The default sort mode of a DataGridViewTextBoxColumn is Automatic.
The default sort mode for other column types is NotSortable.
u can set SortMode property of the column to DataGridViewColumnSortMode .Automatic ,
so if the ValueType is DateTime it will sort the column accordingly.
The default sort mode of a DataGridViewTextBoxColumn is Automatic.
The default sort mode for other column types is NotSortable.
u can set SortMode property of the column to DataGridViewColumnSortMode
so if the ValueType is DateTime it will sort the column accordingly.
cobolinx,
Can you show me how you are populating the DataGridView?
Can you show me how you are populating the DataGridView?
ASKER
I have it set to automatic.
If dssubmissions.Tables(0).Ro ws.Count > 0 Then
Dim i As Integer = 0
While i < dssubmissions.Tables(0).Ro ws.Count
dgvsubmissions.Rows.Add(No thing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
dgvsubmissions.Rows(i).Cel ls("Sub ID").Value = dssubmissions.Tables(0).Ro ws(i).Item ("Submissi on ID").ToString
dgvsubmissions.Rows(i).Cel ls("PRO_ID ").Value = dssubmissions.Tables(0).Ro ws(i).Item ("PRO_ID") .ToString
dgvsubmissions.Rows(i).Cel ls("PrO_NA ME").Value = dssubmissions.Tables(0).Ro ws(i).Item ("PRONAME" ).ToString
Try
dgvsubmissions.Rows(i).Cel ls("Type of Submission").Value = dssubmissions.Tables(0).Ro ws(i).Item ("Type of Submission").ToString()
Catch ex As Exception
Console.Write(ex.Message)
End Try
If dssubmissions.Tables(0).Ro ws(i).Item ("Received Date").ToString().Trim <> "" Then
dgvsubmissions.Rows(i).Cel ls("Receiv ed Date").Value = CDate(dssubmissions.Tables (0).Rows(i ).Item("Re ceived Date")).ToShortDateString
End If
If dssubmissions.Tables(0).Ro ws(i).Item ("Terminat ion Date").ToString().Trim <> "" Then
dgvsubmissions.Rows(i).Cel ls("Termin ation Date").Value = CDate(dssubmissions.Tables (0).Rows(i ).Item("Te rmination Date")).ToShortDateString
End If
dgvsubmissions.Rows(i).Rea dOnly = True
i += 1
End While
End If
If dssubmissions.Tables(0).Ro
Dim i As Integer = 0
While i < dssubmissions.Tables(0).Ro
dgvsubmissions.Rows.Add(No
dgvsubmissions.Rows(i).Cel
dgvsubmissions.Rows(i).Cel
dgvsubmissions.Rows(i).Cel
Try
dgvsubmissions.Rows(i).Cel
Catch ex As Exception
Console.Write(ex.Message)
End Try
If dssubmissions.Tables(0).Ro
dgvsubmissions.Rows(i).Cel
End If
If dssubmissions.Tables(0).Ro
dgvsubmissions.Rows(i).Cel
End If
dgvsubmissions.Rows(i).Rea
i += 1
End While
End If
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can define a custom sort compare with the DataGridView.Sort method:
dgvsubmissions.Sort(New DateTimeComparer())
dgvsubmissions.Sort(New DateTimeComparer())
Private Class DateTimeComparer
Implements IComparer(Of DateTime)
Public Function Compare(ByVal x As Date, ByVal y As Date) As Integer Implements System.Collections.Generic.IComparer(Of Date).Compare
Return Date.Compare(x, y)
End Function
End Class
@TheLearnedOne
do i have 2 use custom sort comparer if the column type is datetime and sortmode is auto?
do i have 2 use custom sort comparer if the column type is datetime and sortmode is auto?
ASKER
I get an unable to cast object of type datetimecompairer to type system.collections.icompai rer
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That was a run-time error.
Here is a better example:
Here is a better example:
Private Class DescendingDateTimeComparer
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim rowX As DataGridViewRow = CType(x, DataGridViewRow)
Dim rowY As DataGridViewRow = CType(y, DataGridViewRow)
Dim dateX As Date = rowX.Cells("DateColumn").Value
Dim dateY As Date = rowY.Cells("DateColumn").Value
Return Date.Compare(dateY, dateX)
End Function
End Class
ASKER
You and sedgwik just made me realize something If I take the ToShortDateString off then it sorts fine.
Open in new window