We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to apply the Custom Grouping while grouping the column using GroupDropArea?

Platform: WPF |
Control: SfDataGrid |
Published Date: November 20, 2014 |
Last Revised Date: June 21, 2021 |
.NET Framework: 4.5
Tags: grouping

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

 

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile