How to use converter for Custom Grouping in WPF Datagrid?
In SfDataGrid, group represents the collection of records that belongs to a particular category. By default the grouping is applied based on the similar data in the Grid but you can customize the grouping by using your own grouping criteria. The Custom Grouping is achieved by assigning GroupColumnDescription converter in XAML or code behind. You can assign a converter that implements IValueConverter with the Custom Grouping logic.
The following code example demonstrates you how to use the converter for Custom Grouping.
C# (Converter)
// DataGrid groups the column against return value of this converter. public object Convert(object value, System.Type targetType, object parameter, CultureInfo culture) { var saleinfo = value as SalesByDate; var dt = DateTime.Now; var days = (int)Math.Floor((dt - saleinfo.Date).TotalDays); var dayofweek = (int)dt.DayOfWeek; var diff = days - dayofweek; if (days <= dayofweek) { if (days == 0) return "TODAY"; if (days == 1) return "YESTERDAY"; return saleinfo.Date.DayOfWeek.ToString().ToUpper(); } if (diff > 0 && diff <= 7) return "LAST WEEK"; if (diff > 7 && diff <= 14) return "TWO WEEKS AGO"; if (diff > 14 && diff <= 21) return "THREE WEEKS AGO"; if (dt.Year == saleinfo.Date.Year && dt.Month == saleinfo.Date.Month) return "EARLIER THIS MONTH"; if (DateTime.Now.AddMonths(-1).Month == saleinfo.Date.Month) return "LAST MONTH"; return "OLDER"; } public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture) { throw new System.NotImplementedException(); } }
The following code example demonstrates you how to add the Custom Grouping by using above converter in XAML.
XAML
<Window.Resources> <local:GroupDataTimeConverter x:Key="customGroupDateTimeConverter" /> </Window.Resources> <!--Assigning customGroupDateTimeConverter in GroupColumnDescription--> <syncfusion:SfDataGrid.GroupColumnDescriptions> <syncfusion:GroupColumnDescription ColumnName="Date" Converter="{StaticResource customGroupDateTimeConverter}" /> </syncfusion:SfDataGrid.GroupColumnDescriptions>
The following code example demonstrates you how to add the Custom Grouping by using above converter in Code behind.
C#
public MainWindow() { //Assigning GroupDataTimeConverter in GroupColumnDescription sfGrid.GroupColumnDescriptions.Add(new GroupColumnDescription() { ColumnName = "Date", Converter= new GroupDataTimeConverter() }); }
The following screenshot shows the Custom Grouping in Grid after assigning the converter in XAML or Code Behind.
But when you group the column into GroupDropArea by drag and drop, the default grouping is only applied. Because, GroupColumnDescription is added internally for this case.
The following screenshot shows you the default grouping while drag and drop the same Date column into GroupDropArea.
To achieve Custom Grouping while drag and drop the column, you need to assign the Converter with custom group logic for specific GroupColumnDescripion in GroupDescriptionsCollectionChanged event. After applying the converter to GroupColumnDescripion, the default grouping is not applied for that column and the Custom Grouping is applied.
The following code example demonstrates you how to assign the converter to GroupColumnDescription in GroupDescriptionsCollectionChanged event.
C#
// To hook GroupDescriptions CollectionChanged event this.sfGrid.View.GroupDescriptions.CollectionChanged += GroupDescriptions_CollectionChanged; void GroupDescriptions_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.NewItems != null) { //Assigning GroupDataTimeConverter in GroupColumnDescription var group = e.NewItems[0] as PropertyGroupDescription; if (group.PropertyName == "Date") group.Converter = new GroupDataTimeConverter(); } }
Sample Links