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.
Unfortunately, activation email could not send to your email. Please try again.

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

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())]);

}

}

 

 

Article ID: Published Date: Last Revised Date: Platform: Control:
2960 11/26/2014 11/26/2014 Silverlight PivotGridControl
Did you find this information helpful?
Add Comment
You must log in to leave a comment

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.