We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

GridGroupingControl and custom dates

I have a GridGroupControl populated with data from a DataTable. One of the table columns is a CustomDate instance. (It knows how to format itself based on user settings in our application. It also implements IComparable so it can be sorted.)

The column is added to the data table as:
myDataTable.Columns.Add("ProcessDate", typeof(CustomDate));

When the date appears in the grid column is is formatted correctly, i.e. "01/25/2008".

If I click on the column heading to sort, the sort seems to be based on the string value in the grid as opposed to the date value of the custom date, therefore the dates don't always appear in the expected order after a sort.

Is there a way to solve this problem?


3 Replies

HA haneefm Syncfusion Team January 28, 2008 11:26 PM UTC

Hi Jay,

You need to implement custom sorting to do this. You can add a custom IComparer to the SortColumnDescriptor for this particular column. This can be done by setting the Comparer property on the SortColumnDescriptor. In the custom IComparer object, you can sort based on any criteria you want.

this.gridGroupingControl1.TableDescriptor.SortedColumns.Changing += new Syncfusion.Collections.ListPropertyChangedEventHandler(SortedColumns_Changing);

void SortedColumns_Changing(object sender, Syncfusion.Collections.ListPropertyChangedEventArgs e)
{
if (e.Action == Syncfusion.Collections.ListPropertyChangedType.Add)
{
SortColumnDescriptor scd = e.Item as SortColumnDescriptor;
if (scd.Name == "Version" && scd.Comparer == null)
{
scd.Comparer = new CustomComparer();
}
}
}

Please refer the below sample that implements this custom sorting for your needs.
SortingSample

Best regards,
Haneef



AS Asterinex January 29, 2008 08:53 AM UTC

Thx, that was also what i needed. Use this code for dates

public class CustomComparerDates : IComparer
{
public int Compare(object x, object y)
{

DateTime d1 = DateTime.Parse(x.ToString());
DateTime d2 = DateTime.Parse(y.ToString());
if (d1 == null || d1 == DateTime.MinValue)
return -1;
else if (d2 == null || d2 == DateTime.MinValue)
return 1;
else
{
return d1.CompareTo(d2);
}



}
}



JD Jay Delp January 29, 2008 08:37 PM UTC

Okay, plugged in the event handler from Haneef and a variation of the Comparer from Gert and all is working as desired.

Thanks.


Loader.
Live Chat Icon For mobile
Up arrow icon