This question is locked. New answers and comments are not allowed.
Hello,
I have a GridView with grouped by a string property. I want the items inside each grup sorted by a datetime property. Data items looks like:
Searching at this forum I found an old thread :http://www.telerik.com/community/forums/silverlight/gridview/how-to-group-and-sort-by-another-column.aspx so I modified the data like follows:
Xaml looks like:
Using this approch doesn't seem to work, CompareTo method on DataItemInfo class is never invoked. Setting SortDirection="Ascending" in GroupDescription xaml definitiion produces a NullReferencedException: Telerik.Windows.Data!Telerik.Windows.Data.IGroupDescriptorExtensions.GetGroupSortKeyFunction...
After some more searching in this forum, I decided to create a custom GroupDescriptor by inhereting from GroupDescriptor, code is as follows:
Now, xaml looks like
Again, this approcah doesn't work. Again, setting SortDirection="Ascending" produces the same exception as before.
Any help?
Thanks
I have a GridView with grouped by a string property. I want the items inside each grup sorted by a datetime property. Data items looks like:
public class DataItem { public string Property1 { get; set; } public string Property2 { get; set; } public DateTime Date { get; set; } }Searching at this forum I found an old thread :http://www.telerik.com/community/forums/silverlight/gridview/how-to-group-and-sort-by-another-column.aspx so I modified the data like follows:
public class DataItem { public DataItem(string p1, string p2, DateTime date) { Property1 = p1; Property2 = p2; Date = date; ItemInfo = new DataItemInfo (p1, date); } public string Property1 { get; set; } public string Property2 { get; set; } public DateTime Date { get; set; } public DataItemInfo ItemInfo { get; set; } } public class DataItemInfo: IComparable { public DataItemInfo(string p1, DateTime date) { Property1 = p1; Date = date; } public string Property1 { get; set; } public DateTime Date { get; set; } #region Implementation of IComparable /// <summary> /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object. /// </summary> /// <returns> /// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance precedes <paramref name="obj"/> in the sort order. Zero This instance occurs in the same position in the sort order as <paramref name="obj"/>.Greater than zero This instance follows <paramref name="obj"/> in the sort order. /// </returns> /// <param name="obj">An object to compare with this instance. </param><exception cref="T:System.ArgumentException"><paramref name="obj"/> is not the same type as this instance. </exception> public int CompareTo(object obj) { return Date.CompareTo(((DataItemInfo) obj).Date); } #endregion public override bool Equals(object obj) { return Property1.Equals(((DataItemInfo) obj).Property1); } public override int GetHashCode() { return Property1.GetHashCode(); } public static bool operator ==(DataItemInfo a, DataItemInfo b) { if (ReferenceEquals(a, b)) return true; if (((object)a == null) || ((object)b == null)) return false; return a.Property1 == b.Property1; } public static bool operator !=(DataItemInfo a, DataItemInfo b) { return !(a == b); } }Xaml looks like:
<telerik:RadGridView AutoGenerateColumns="False"> <telerik:RadGridView.Columns> <telerik:GridViewDataColumn Header="Property2" DataMemberBinding="{Binding Path=Property2}"/> <telerik:GridViewDataColumn Header="Date" DataMemberBinding="{Binding Path=Date}"/> </telerik:RadGridView.Columns> <telerik:RadGridView.GroupDescriptors> <data:GroupDescriptor Member="ItemInfo"/> </telerik:RadGridView.GroupDescriptors> </telerik:RadGridView>Using this approch doesn't seem to work, CompareTo method on DataItemInfo class is never invoked. Setting SortDirection="Ascending" in GroupDescription xaml definitiion produces a NullReferencedException: Telerik.Windows.Data!Telerik.Windows.Data.IGroupDescriptorExtensions.GetGroupSortKeyFunction...
After some more searching in this forum, I decided to create a custom GroupDescriptor by inhereting from GroupDescriptor, code is as follows:
public class SortingGroupDescriptor : GroupDescriptor { public AggregateFunction Sorting { get; set; } public override Expression CreateGroupSortExpression(Expression groupingExpression) { var expr = base.CreateGroupSortExpression(groupingExpression); expr = Expression.Property(expr, "Date"); return expr; } }Now, xaml looks like
<telerik:RadGridView AutoGenerateColumns="False"> <telerik:RadGridView.Columns> <telerik:GridViewDataColumn Header="Property2" DataMemberBinding="{Binding Path=Property2}"/> <telerik:GridViewDataColumn Header="Date" DataMemberBinding="{Binding Path=Date}"/> </telerik:RadGridView.Columns> <telerik:RadGridView.GroupDescriptors> <local:SortingGroupDescriptor Member="ItemInfo"/> </telerik:RadGridView.GroupDescriptors> </telerik:RadGridView>Again, this approcah doesn't work. Again, setting SortDirection="Ascending" produces the same exception as before.
Any help?
Thanks