Articles in this section
Category / Section

How to maintain expanded state of groups in printing in WPF DataGrid (SfDataGrid)?

3 mins read

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


Conclusion

I hope you enjoyed learning about how to maintain expanded state of groups in printing in WPF DataGrid (SfDataGrid).

You can refer to our WPF DataGrid 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 DataGrid 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