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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to make the Date columns in PivotGrid to sort according to Date chronological order

Platform: Silverlight |
Control: PivotGridControl |
Published Date: November 26, 2014 |
Last Revised Date: November 26, 2014

We have added a new “DateComparer” class which implements the IComparer Interface in which the dates gets compared and sorted according to the Format specified. And this Comparer should be included to a specific Pivot Item (i.e., Pivot Item which has “DateTime” field as FieldMappingName), so that the dates gets sorted accordingly.

Please refer the below code snippet which shows the “DateComparer” class.

C#

public class DateComparer : IComparer

{

Dictionary<string, int> lookUp = null;

string fmt = string.Empty;

public DateComparer(string format)

{

fmt = string.Format("{0}", format);

lookUp = new Dictionary<string, int>();

DateTime dt0 = Convert.ToDateTime("2013/12/01");

DateTime dt1 = Convert.ToDateTime("2014/01/08");

double days = (dt1 - dt0).TotalDays;

for (int j = 0; j <= days; j++)

{

DateTime dt = dt0.AddDays(j);

if (!lookUp.ContainsKey(dt.ToString(fmt)))

{

lookUp.Add(dt.ToString(fmt), j);

}

}

}

public int Compare(object x, object y)

{

DateTime x1, y1;

if (DateTime.TryParse(x.ToString(), out x1) && DateTime.TryParse(y.ToString(), out y1))

{

x = x1.ToString(fmt);

y = y1.ToString(fmt);

}

return lookUp[x.ToString()].CompareTo(lookUp[(y.ToString())]);

}

}

 

 

ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon