I've got a DataGridView which, amongst others, has columns containing dates. Unfortunately, the dates are in the format DD.MM.YYYY and the whole value is in 1 column, which is the common date format here in Europe. The DGV is tied to a BindingSource which is capable of sorting and advanced sorting.
The problem is the following: If I just use the standard sorting of the DGV, the dates are viewed as strings (they're displayed in a DataGridViewTextBoxColumn) and thus sorted by day->month->year but of course I'd want exactly the opposite; I want them sorted chronologically.
So, is there a way to sort this columns the way I want them to?
What options do I have to achiev what I want? When explaining, please keep in mind that I'm -althoug not new to programming- new to this Windows Forms stuff.
Thanks in advance!
private void DataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
if (e.Column.Name == "YourColumnName") //<== this must be your date column name
{
DateTime dt1 = Convert.ToDateTime(e.CellValue1);
DateTime dt2 = Convert.ToDateTime(e.CellValue2);
e.SortResult = System.DateTime.Compare(dt1, dt2);
e.Handled = true;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With