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 page ("Grid") to the Ribbon toolbar using the Ribbon Control Designer. To learn how to manage ribbon items, see the Ribbon Items Page topic.
  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.20.1: 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.

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