Skip to main content
A newer version of this page is available. .
All docs
V21.2

How to: Print a Document that Contains Pivot and Chart Controls

  • 4 minutes to read

The following example shows how to print a document that contains a Pivot Grid control with an integrated Chart:

pivot integrated with chart print preview

View Example: WinForms - How to: Print a Document that Contains Pivot and Chart Controls

  1. Create the CreateCompositeLink method that returns a CompositeLink object. CompositeLink combines different printing links into one document, shows its print preview, and prints or exports the resulting document. Create two PrintableComponentLink objects for the Pivot and Chart controls, and add these objects to the link collection to combine them in CompositeLink.

    using DevExpress.XtraCharts;
    using DevExpress.XtraPivotGrid;
    using DevExpress.XtraPrinting;
    using DevExpress.XtraPrintingLinks;
    using System;
    using System.Windows.Forms;
    
    namespace WinFormsExport {
        public partial class Form1 : DevExpress.XtraEditors.XtraForm {
            public Form1() {
            // ...
            }
            // ...
            private CompositeLink CreateCompositeLink(PivotGridControl pivotGridControl1, ChartControl chartControl1) {
                PrintableComponentLink pivotLink = new PrintableComponentLink();
                pivotLink.Component = pivotGridControl1;
                PrintableComponentLink chartLink = new PrintableComponentLink();
                chartLink.Component = chartControl1;
                CompositeLink compositeLink = new CompositeLink(new PrintingSystem());
                compositeLink.Links.AddRange(new object[] { pivotLink, chartLink });
                return compositeLink;
                // ...
        }
    }
    
  2. Add the MenuStrip control to the form. Create two toolStripMenuItem objects and handle their Click events. Call the ShowPreview and ExportToXlsx methods of the composite link in the Click event handlers.

    using DevExpress.XtraCharts;
    using DevExpress.XtraPivotGrid;
    using DevExpress.XtraPrinting;
    using DevExpress.XtraPrintingLinks;
    using System;
    using System.Windows.Forms;
    
    namespace WinFormsExport {
        public partial class Form1 : DevExpress.XtraEditors.XtraForm {
            public Form1() {
                InitializeComponent();
                ToolStripMenuItem toolStripMenuItem1 = new ToolStripMenuItem();
                ToolStripMenuItem toolStripMenuItem2 = new ToolStripMenuItem();
                menuStrip1.Items.AddRange(new ToolStripItem[] {
                    toolStripMenuItem1,
                    toolStripMenuItem2 });
                toolStripMenuItem1.Name = "Print Preview";
                toolStripMenuItem1.Text = "Print Preview";
                toolStripMenuItem2.Name = "Export to XLSX";
                toolStripMenuItem2.Text = "Export to XLSX";
    
                toolStripMenuItem1.Click += toolsToolStripMenuItem2_Click;
                toolStripMenuItem2.Click += toolsToolStripMenuItem_Click;
    
                // This line of code is generated by Data Source Configuration Wizard
                // Fill the ExcelDataSource asynchronously
                excelDataSource1.FillAsync();
            }
            // ...
            private void toolsToolStripMenuItem_Click(object sender, EventArgs e) {
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.Filter = "Excel Workbook|*.xlsx";
                saveFileDialog1.ShowDialog();
                if (saveFileDialog1.FileName != "") {
                    CompositeLink compositeLink = CreateCompositeLink(pivotGridControl1, chartControl1);
                    compositeLink.ExportToXlsx(saveFileDialog1.FileName);
                }
            }
            private void toolsToolStripMenuItem2_Click(object sender, EventArgs e) {
                CompositeLink compositeLink = CreateCompositeLink(pivotGridControl1, chartControl1);
                compositeLink.ShowPreview();
            }
        }
    }
    

Run the application and use the Print Preview and Export to XLSX commands to print a document that contains both controls:

form with printpreview and export commands

See Also