Articles in this section
Category / Section

How to use converter for Custom Grouping in WPF Datagrid?

5 mins read

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.

G:\Serialization KB\Custom Grouping\After.PNG

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.

G:\Serialization KB\Custom Grouping\Before.PNG

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

 

WPF

WRT

SilverLight

UWP

 
 Conclusion
I hope you enjoyed learning how to use converter for Custom Grouping in WPF Datagrid.
You can refer to our WPF Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF Grid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied