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
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; }
}
}
}