This is a migrated thread and some comments may be shown as answers.

Individual sorting per group

1 Answer 79 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jasper
Top achievements
Rank 1
Jasper asked on 14 Oct 2014, 10:32 AM
Hi

We are trying to do custom sorting per grouping in a datagrid and I've been unable to find any examples. Say we have 2 groups, foo and bar, and we want to sort the row on 2 different properties. The groups are, in this particular example, known and I somehow like to specify a GroupSortingExpression per grouping. 

In the attached screenshot, I would like to sort the group 'group-1' by foo and the group 'group-2' by bar. Is this possible?

Any hints are greatly appreciated!
Regards
/Jasper

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Telerik.Windows.Data;
 
namespace WpfApplication2
{
    public class DataItemCollection
    {
        private ObservableCollection<DataItem> _internalItems;
        public ObservableCollection<DataItem> Items
        {
            get
            {
                return _internalItems ?? (_internalItems = new ObservableCollection<DataItem>
                {
                    new DataItem("group-1", "bar-1", "foo-1", new DateTime(2000, 01, 2)),
                    new DataItem("group-1", "bar-3", "foo-3", new DateTime(2000, 01, 1)),
                    new DataItem("group-1", "bar-2", "foo-0", new DateTime(2000, 01, 1)),
                    new DataItem("group-2", "bar-4", "foo-4", new DateTime(2000, 01, 1)),
                    new DataItem("group-2", "bar-2", "foo-2", new DateTime(2000, 01, 4)),
                    new DataItem("group-2", "bar-5", "foo-5", new DateTime(2000, 01, 3))
                });
            }
        }
    }
 
    public class DataItem
    {
        public DataItem(string @group, string bar, string foo, DateTime date)
        {
            Group = @group;
            Bar = bar;
            Foo = foo;
            Date = date;
 
            GroupingInfo = new DataGroupingInfo(@group);
 
             
        }
 
        public string Foo { get; set; }
 
        public string Group { get; set; }
 
        public string Bar { get; set; }
 
        public DateTime Date { get; set; }
 
        public DataGroupingInfo GroupingInfo { get; set; }
    }
 
    public class DataGroupingInfo : IEquatable<DataGroupingInfo>, IComparable<DataGroupingInfo>, IComparable
    {
        public DataGroupingInfo(string p1)
        {
            Property1 = p1;
        }
 
        public string Property1 { get; set; }
 
        public bool Equals(DataGroupingInfo other)
        {
            return other.Property1 == Property1;
        }
 
        public int CompareTo(DataGroupingInfo other)
        {
            return String.Compare(Property1, other.Property1, StringComparison.Ordinal);
        }
 
        public override int GetHashCode()
        {
            return (Property1 != null ? Property1.GetHashCode() : 0);
        }
 
        public int CompareTo(object obj)
        {
            return CompareTo((DataGroupingInfo) obj);
        }
    }
 
    public class DataItemInfoGroupDescriptor : GroupDescriptor<DataItem, DataGroupingInfo, string>
    {
        public DataItemInfoGroupDescriptor()
        {
            GroupingExpression = t => t.GroupingInfo;
             
            GroupSortingExpression = g => g.Key.Property1;
        }
 
        public override ListSortDirection? SortDirection
        {
            get { return ListSortDirection.Ascending; }
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 15 Oct 2014, 06:51 AM
Hello Jasper,

Such an example is available in the following Sorting the groups  forum thread. Would you please try it and let me know how does it work for you? 


Regards,
Dimitrina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Jasper
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Share this question
or