I have the
following problem:
I have a grouping
where as the unterlying data filed for grouping is a date field. The field used
for grouping is hiden in the grids view. The column has a format wheres it
should only show the year and the month, e.g. 2020 January. If I configure the columns format like that the sorting
will be done according to the visible value in the group header.
The main
problem seems to me, that it is not possible to format a value in the group
header text similar to the column (e.g {key:yyyy MMMM})
How can I
achive a different value in the group heading but remaining the sorting?
At the moment I added the a sort criteria like <year><month> at the begining to keep the sorting. But this looks not very nice in a report.
Regards,
|
<ContentPage.Resources>
<ResourceDictionary>
<local:GroupConverter x:Key="groupConverter" />
<local:CustomSortComparer x:Key="sortComparer"/>
</ResourceDictionary>
</ContentPage.Resources>
<sfgrid:SfDataGrid x:Name="sfGrid"
AllowSorting="True"
ColumnSizer="Star"
AllowDraggingRow="True"
AllowResizingColumn="True"
SelectionMode="Single"
AllowEditing="True"
NavigationMode="Cell"
AllowKeyboardNavigation="True"
ItemsSource="{Binding OrdersInfo}"
ShowColumnWhenGrouped="False">
<sfgrid:SfDataGrid.GroupColumnDescriptions>
<sfgrid:GroupColumnDescription ColumnName="ShipDate" Comparer="{StaticResource sortComparer}" Converter="{StaticResource groupConverter}" />
</sfgrid:SfDataGrid.GroupColumnDescriptions>
</sfgrid:SfDataGrid> |
|
public class CustomSortComparer : IComparer<object>, ISortDirection
{
public ListSortDirection SortDirection { get; set; }
public CustomSortComparer()
{
}
public int Compare(object x, object y)
{
DateTime dateX = DateTime.MaxValue;
DateTime dateY = DateTime.MaxValue;
if (x.GetType() == typeof(OrderInfo))
{
dateX = ((OrderInfo)x).ShipDate;
dateY = ((OrderInfo)y).ShipDate;
}
else if (x.GetType() == typeof(Group))
{
dateX = DateTime.ParseExact(((Group)x).Key.ToString(), "yyyy MMMM", CultureInfo.InvariantCulture);
dateY = DateTime.ParseExact(((Group)y).Key.ToString(), "yyyy MMMM", CultureInfo.InvariantCulture);
}
else
{
dateX = (DateTime)x;
dateY = (DateTime)y;
}
if (DateTime.Compare(dateX, dateY) >= 0)
return SortDirection == ListSortDirection.Ascending ? 1 : -1;
else
return SortDirection == ListSortDirection.Ascending ? -1 : 1;
}
} |
Hi
Many thanks for the proposed solution. With the custom comparer I was able to sort according to the date.
Regards,
Bruno