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)); } }
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);
Hi Mauro,
Thanks so much for your reply. Actually it fixes my problem half way now.
From the above, it only sorts the grid rows. and it doesn't sort the underlying datasource for the bindingsource.
To resolve that i tried setting the HeaderClickAction to ExternalSortMulti and on the ugPatientBirthday_AfterSortChange i have set the following:
private void ugPatientBirthday_AfterSortChange(object sender, Infragistics.Win.UltraWinGrid.BandEventArgs e) { try { if (e.Band.SortedColumns["date_of_birth"].SortIndicator == SortIndicator.Ascending) bindingSourcePatientBirthdayReport.Sort = "DAY(date_of_birth) ASC, MONTH(date_of_birth) ASC, YEAR(date_of_birth) ASC" } catch (Exception) { throw; } }
This gives me an error saying: "Sort string contains a property that is not in the IBindingList" which I assume the keys "DAY", " MONTH" and "YEAR" is not recognizing.
My issue is, once i sort the grid using header click, and hit the print button, the underlying data source should be passed on to a report (which includes the new header click sorting).
Please let me know a solution for this so I can use the AfterSortChange to sort the underlying data source.
Need response ASAP!!
Thanks so much!