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 maintain expanded state of groups in printing in WPF DataGrid (SfDataGrid)?

Platform: WPF |
Control: SfDataGrid

All the groups will be printed in expanded state by default in WPF DataGrid (SfDataGrid). You can also avoid this and maintain the expanded states of group as in view when printing by inheriting the GridPrintManager and overriding the GetSourceListForPrinting method.

 

Refer to the following code sample for assigning source list for printing.

 

dataGrid.PrintSettings.PrintManagerBase = new CustomPrintManagerBase(dataGrid);
dataGrid.ShowPrintPreview();
 
public class CustomPrintManagerBase : GridPrintManager
{
      public CustomPrintManagerBase(SfDataGrid dataGrid) : base(dataGrid)
      {
 
      }
 
      protected override IList GetSourceListForPrinting()
      {
            if (View.GroupDescriptions.Any())
            {
                var records = ToIEnumerable(View.TopLevelGroup.GetEnumerator()).ToList();
                List<NodeEntry> expandedGroups = new List<NodeEntry>();
                foreach(var record in records)
                {
                    if(record is RecordEntry)
                    {
                        var group = record.Parent as Group;
                        if (group != null && group.IsExpanded)
                            expandedGroups.Add(record);
                    }
                    else if(record is Group)
                    {
                        var group = record as Group;
                        if(group.IsTopLevelGroup || group.Level == 1)
                        {
                            expandedGroups.Add(group);
                        }
                        //For Multi-ColumnGrouping
                        else 
                        {
                            var parentGroup = group.Parent as Group;
                            if (parentGroup != null && parentGroup.IsExpanded)
                                expandedGroups.Add(group);
                        }
                    }
                }
                return expandedGroups;
            }
            return base.GetSourceListForPrinting();
      }
}

 

 

View WPF DataGrid Grouping Demo in GitHub

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