Skip to main content

How to: Use the PrintableComponentLink to Print DevExpress Controls

  • 3 minutes to read

This tutorial illustrates how to use the PrintableComponentLink to print DevExpress Windows Forms controls at runtime. For a design-time approach, refer to the How to: Preview, Export and Print a Windows Forms Control topic.

To get started with this tutorial, create a Windows Forms Application with a print preview, similar to the one created in the following tutorial: How to: Add a Print Preview to a Windows Forms Application.

This tutorial consists of the following sections.

Add a Grid Control

  1. Add a new Ribbon page (“Grid“) using the Toolbars tab in the Ribbon Designer.
  2. To add a grid to the main form, press CTRL+ALT+X to open the Toolbox. Next, drag the GridControl control from the DX.23.2: Data & Analytics category and drop it on the form.

    printing-preview-winforms-control00

  3. Bind the grid control to data. To learn how to bind the grid control to data sources of different types, see the Examples: Data Binding topic.
  4. To use the Ribbon control’s pages to quickly switch between the grid and print preview, handle the RibbonControl.SelectedPageChanged event, and add the following code to the event handler.

    private void ribbonControl1_SelectedPageChanged(object sender, EventArgs e) {
        switch(ribbonControl1.SelectedPage.Name) {
            case "Grid":
                documentViewer1.Visible = false;
                gridControl1.Visible = true;
                break;
            case "printPreview":
                gridControl1.Visible = false;
                documentViewer1.Visible = true;
                printableComponentLink1.CreateDocument();
                break;
        }
    }
    

If you use printing system links to print or export data from a view, you should set the view’s OptionsPrint.ShowPrintExportProgress property to false. Additionally, if the server mode is used, it is necessary to call the BaseView.ClearDocument method before printing or exporting data.

The following code demonstrates how to create a PrintableComponentLink, add it to the PrintingSystem.Links collection, adjust its printing settings, and use it to print an existing GridControl. Use the grid’s OptionsPrint property to customize its printing settings.

using DevExpress.XtraPrinting;
using DevExpress.XtraGrid.Views.Grid;
// ...

// Create printing components.
PrintingSystem printingSystem1 = new PrintingSystem();
PrintableComponentLink printableComponentLink1 = new PrintableComponentLink();
// ...

private void Form1_Load(object sender, EventArgs e) {
    // Add the link to the printing system's collection of links.
    printingSystem1.Links.AddRange(new object[] { printableComponentLink1 });

    // Assign a control to be printed by this link.
    printableComponentLink1.Component = gridControl1;

    // Assign the printing system to the document viewer.
    documentViewer1.PrintingSystem = printingSystem1;
}

Publish the Document

A Windows Forms application with a print preview is now ready. You can change the GridControl layout by applying grouping, filtering or sorting.

printing-preview-winforms-control04

To preview the document, click the Print Preview tab on the Ribbon. Use the menu options in this tab to print and/or export the document to one of the available formats.

printing-preview-winforms-control05

Use CompositeLink to combine several printing links together into a composite document and print the document. Read the following help topic for detailed information and examples: How to: Combine Links via the CompositeLink.