Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
740
Sort Ultragrid with Date Column
posted

Hi there,

I have following fields displayed in a gird and sorted out by day, month and year ascending as stated below in the ORDER BY clause.

SELECT

title,first_name,surname,date_of_birth,age,street,suburb,state,postcode

FROM #temp_bday

ORDER BY DAY(date_of_birth) ASC, MONTH(date_of_birth) ASC, YEAR(date_of_birth) ASC

When the data loads it shows the sorting correctly in the grid. But my other requirement is, If the DOB header is selected (to sort the data manually by this field) use the same rule to sort the data. (Rule : ORDER BY DAY(date_of_birth) ASC, MONTH(date_of_birth) ASC, YEAR(date_of_birth) ASC)

I've tried to use the SortComparer for this, but i cant seems to find a way to sort the field by parts of the date.

Could I please get some help from anyone for this?? I have done string & integer compares with SortComparer but cant seem to think of a solution to sort datetime as parts of the date (eg. day, month and year)

If someone can provide a Sample code, that would be great!!! Need it ASAP!!

Thanks in Advance!!

PS: Sample for Datetime compare i did ( which is not working for the above rule)

//init gird//

ultragrid1.DisplayLayout.Bands[0].Columns["date_of_birth"].SortComparer = new dateComparer();

public class dateComparer : IComparer

{
public dateComparer()
{
}

public int Compare(object x, object y)
{
UltraGridCell xCell = (UltraGridCell)x;
UltraGridCell yCell = (UltraGridCell)y;

return DateTime.Compare(Convert.ToDateTime(xCell.Row.Cells["date_of_birth"].Value), Convert.ToDateTime(yCell.Row.Cells["date_of_birth"].Value));
}
}

Parents
  • 355
    Suggested Answer
    Offline posted

    Hello,

    i think that your errror is the custom compare method:

    it's based on default datettime comparer, while you should implement your custom logic:

    try to modify the method as follow:

    var date1 = (DateTime) ((UltraGridCell) x).Value;
    var date2 = (DateTime)((UltraGridCell)y).Value;
    if (date1.Day < date2.Day)
    return -1;
    if (date2.Day < date1.Day)
    return 1;

    //days equals compare months
    if (date1.Month < date2.Month)
    return -1;
    if (date2.Month < date1.Month)
    return 1;

    //at the end compare years
    return date1.Year.CompareTo(date2.Year);

Reply Children