Printing

SfDataGrid control allows you to print the data displayed in the SfDataGrid. The Print Manager of SfDataGrid is designed to support different orientations, sizes, margins, etc. You can change print settings using the PrintSettings property of SfDataGrid. The following section covers all different API sets, print preview options and methods in printing feature.

Overview

EssentialSfDataGrid for WinRT provides in-built support for print and print preview.You can enable the printing features inSfDataGrid control. You can print SfDataGrid control in two ways

  • Invoke the public method SfDataGrid.Print() to print the instance of the SfDataGrid with print preview .

The following table lists the methods available in SfDataGrid for printing support feature.

Method Prototype Description
SfDataGrid.Print() Print() Invoked to print the data displayed in SfDataGrid with default settings (print preview window is not displayed).

The following table lists the SfDataGrid properties in SfDataGrid’s PrintSettings.

Property Name Type Description Default Value
SfDataGrid.PrintSettings.AllowColumnWidthFitToPrintPage Boolean Gets or sets whether to allow the columns to fit in a printable page.\ or not. True
SfDataGrid.PrintSettings.AllowRepeatHeaders Boolean Gets or sets whether the column header is displayed in the printable pages or not. True
SfDataGrid.PrintSettings.PrintPageFooterHeight Double Gets or sets the height for the footer template in the printable page. 0
SfDataGrid.PrintSettings.PrintPagerFooterTemplate DataTemplate Gets or sets the footer template for printable page. Null
SfDataGrid.PrintSettings.PrintPageHeaderHeight Double Gets or sets the height for the header template in the printable page. 0
SfDataGrid.PrintSettings.PrintPageHeaderTemplate DataTemplate Gets or sets the header template for printable page. Null
SfDataGrid.PrintSettings.PrintPageMargin Thickness Gets or sets the page margin for the printable page. new Thickness(96)
SfDataGrid.PrintSettings.PrintPageWidth Double Gets or sets the width for printable page. 793.90d
SfDataGrid.PrintSettings.PrintManagerBase.PrintPageHeight Double Gets or sets the height for the printable page. 1122.52d
SfDataGrid.PrintSettings.PrintManagerBase.PrintPageHeaderTemplate DataTemplate Gets or sets the header template for the printable page. Null
SfDataGrid.PrintSettings.PrintManagerBase.PrintPageOrientation PrintOrientation Gets or sets the orientation for printing. PrintOrientation.Portrait
SfDataGrid.PrintSettings.PrintManagerBase.PrintScaleOption PrintScaleOptions Gets or sets the print scale options for printing. PrintScaleOptions.NoScaling
SfDataGrid.PrintSettings.PrintManagerBase.PrintRowHeight Double Gets or sets the print row height 24

The following code example shows you how to use Printing feature in SfDataGrid with Print Preview.

  • xaml
  • <Page.DataContext>
    
    <local:ViewModel/>
    
    </Page.DataContext>
    
    
    
    <syncfusion:SfDataGrid x:Name="syncgrid"
    
                           AutoGenerateColumns="True"
    
                           ItemsSource="{Binding EmployeeDetails}"
    
                           NavigationMode="Row">
  • c#
  • private void PrintGrid(object sender, RoutedEventArgs args)
    
    {
    
         if (dataGrid == null) 
    
             return;
    
         dataGrid.Print();
    
    }

    The following screenshot displays the SfDataGrid in print preview mode.

    Custom Printing

    The default SfDataGrid.ShowPrintPreview () provides you the multiple options in printing. It also provides the flexibility to write CustomPrintManager. The descendant of Custom Print Manager is derived from GridPrintManager based on the required level of customization method that is overridden.

    The following table lists all the override methods of GridPrintManager.

    </tr> </table> The following code example shows you how a CustomPrintManager is developed using the override methods in Print Manager.
  • c#
  • public class CustomPrintManager : GridPrintManager
    
        {
    
            private SfDataGrid dataGrid;
    
    
    
            public CustomPrintManager(SfDataGrid grid)
    
                : base(grid)
    
            {
    
                dataGrid = grid;
    
           }
    
            /// <summary>
    
            /// Invokes to Get the column width for the corresponding Mapping name.
    
            /// </summary>
    
            /// <param name="mappingName"></param>
    
            /// <returns>double</returns>
    
            protected override double GetColumnWidth(string mappingName)
    
            {
    
                return (dataGrid.PrintSettings.PrintPageWidth -
    
                        (dataGrid.PrintSettings.PrintPageMargin.Left + dataGrid.PrintSettings.PrintPageMargin.Right + 20))/4;
    
            }
    
    
    
            /// <summary>
    
            /// Invokes to Get the Column's List that need to be printed.
    
            /// </summary>
    
            /// <returns></returns>
    
            /// <remarks>List<string></remarks>
    
            protected override System.Collections.Generic.List<string> GetColumnNames()
    
            {
    
                return this.dataGrid.Columns.Select(x => x.MappingName).ToList();
    
            }
    
    
    
            public override PrintGridCell GetPrintGridCell(object record, string mappingName)
    
            {
    
                var index = dataGrid.View.Records.IndexOfRecord(record);
    
                if (index%2 == 0)
    
                    return new PrintGridCell() {Background = new SolidColorBrush(Colors.Bisque)};
    
                return base.GetPrintGridCell(record, mappingName);
    
            }
    
    
    
            /// <summary>
    
            /// Invokes to get the Column's Padding corresponding to the column's Mapping name.
    
            /// </summary>
    
            /// <param name="mappingName"></param>
    
            /// <returns>Thickness</returns>
    
            protected override Thickness GetColumnPadding(string mappingName)
    
            {
    
                var padding = dataGrid.Columns[mappingName].ReadLocalValue(GridColumn.PaddingProperty);
    
                return padding != DependencyProperty.UnsetValue
    
                              ? new Thickness(4)
    
                              : new Thickness(2, 2, 6, 6);
    
            }
    
    
    
            /// <summary>
    
            /// Invokes to get the column's Header Text
    
            /// </summary>
    
            /// <param name="mappingName"></param>
    
            /// <returns>string</returns>
    
            protected override string GetColumnHeaderText(string mappingName)
    
            {
    
                if (mappingName == "OrderID")
    
                    return "ID";
    
                return base.GetColumnHeaderText(mappingName);
    
            }
    
    
    
            /// <summary>
    
            /// Invokes to get the Column Text alignment corresponding with the Mapping name.
    
            /// </summary>
    
            /// <param name="mappingName"></param>
    
            /// <returns>TextAlignment</returns>
    
            protected override TextAlignment GetColumnTextAlignment(string mappingName)
    
            {
    
                if (mappingName == "Country")
    
                    return TextAlignment.Right;
    
                return base.GetColumnTextAlignment(mappingName);
    
            }
    
    
    
        }
    
    }
    When Custom Print Manager is created, you can assign it to PrintManagerBase to load the CustomPrintManager.
  • c#
  • dataGrid.PrintSettings.PrintManagerBase = new CustomPrintManager(dataGrid);
    
    dataGrid.Print();
    The following screenshot displays you how SfDataGrid is printed. ![](Printing_images/Printing_img2.png)
    Method Prototype Description
    GridPrintManager.InitializeProperties() InitializeProperties() Invoked to initialize all the print options for printing in SfDataGrid
    GridPrintManager.GetColumnNames() List GetColumnNames()</td> Invoked to get the list of columns that is printed.
    GridPrintManager.GetColumnWidth() double GetColumnWidth(string mappingName) Invoked to get the column width for corresponding Mapping Name
    GridPrintManager.GetColumnHeaderText() string GetColumnHeaderText(string mappingName) Invoked to get the columns’s Header Text.
    GridPrintManager.GetFormattedCellValue() string GetFormatedCellValue(object cellValue, string mappingName) Invoked to get the formatted cell value from the column.
    GridPrintManager.GetColumnPadding() Thickness GetColumnPadding(string mappingName) Invoked to get the column’s padding corresponding to the column’s Mapping Name.
    GridPrintManager.GetColumnTextAlignment() TextAlignment GetColumnTextAlignment(string mappingName) Invoked to get column’s text alignment corresponding to the column’s Mapping Name.
    GridPrintManager.GetColumnTextWrapping() TextWrapping GetColumnTextWrapping(string mappingName) Invoked to get the column’s text alignment corresponding to the column’s Mapping Name.
    GridPrintManager.GetColumnElement() object GetColumnElement(object record, string mappingName) Invoked to get the column’s element to set content for the Print Grid Cell.
    GridPrintManager.GetColumnHeaderElement() UIElement GetColumnHeaderElement(string mappingName) Invoked to get the Header column element to set content for the Print Header Cell
    GridPrintManager.GetGroupCaptionStringFormat() string GetGroupCaptionStringFormat() Invoked to get the Group Caption String Format
    GridPrintManager.GetPrintGridCell() PrintGridCell GetPrintGridCell(object record, string mappingName) Invoked to return new instance of PrintGridCell