Back to Devexpress

Advanced Grid Printing and Exporting

windowsforms-114962-controls-and-libraries-data-grid-export-and-printing-advanced-grid-printing-and-exporting.md

latest9.6 KB
Original Source

Advanced Grid Printing and Exporting

  • Sep 30, 2024
  • 5 minutes to read

The WinForms Report Generator components allows you to convert the DevExpress WinForms Grid control to a report. You can generate reports for the following view types:

Supported Grid Features

Generate a Report from Grid at Design Time

Follow the steps below to generate a report at design time:

  1. Drop the ReportGenerator component from the Toolbox onto a Form.

  2. Open the ReportGenerator’s smart tag menu and click “Generate Report”:

  3. Select the source View and click Next :

  4. Specify whether to display column headers and a footer with total summaries. Click Next to proceed:

  5. Specify whether to display group rows and footers with group summaries:

  6. Configure the following appearance settings:

  7. Specify a name for the report, then click Finish.

Generate a Report from Grid in Code

Using the ReportGenerator.GenerateReport static method to generate a report:

csharp
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.ReportGeneration;
using DevExpress.Utils;
using System;

namespace DXApplication {
    public partial class Form1 : DevExpress.XtraBars.Ribbon.RibbonForm {
        XtraReport dataGridReport;
        public Form1() {
            InitializeComponent();
            gridControl1.ForceInitialize();
            dataGridReport = ReportGenerator.GenerateReport(gridView1,
                new ReportGenerationOptions() {
                    EnablePrintAppearanceEvenRow = DefaultBoolean.True,
                    EnablePrintAppearanceOddRow = DefaultBoolean.True,
                    PrintColumnHeaders = DefaultBoolean.True,
                    PrintVerticalLines = DefaultBoolean.True,
                    PrintHorizontalLines = DefaultBoolean.False
                });
        }
        //...
    }
}
vb
Imports DevExpress.XtraReports.UI
Imports DevExpress.XtraReports.ReportGeneration
Imports DevExpress.Utils
Imports System

Namespace DXApplication
    Partial Public Class Form1
        Inherits DevExpress.XtraBars.Ribbon.RibbonForm

        Private dataGridReport As XtraReport
        Public Sub New()
            InitializeComponent()
            gridControl1.ForceInitialize()
            dataGridReport = ReportGenerator.GenerateReport(gridView1, New ReportGenerationOptions() With {
                .EnablePrintAppearanceEvenRow = DefaultBoolean.True,
                .EnablePrintAppearanceOddRow = DefaultBoolean.True,
                .PrintColumnHeaders = DefaultBoolean.True,
                .PrintVerticalLines = DefaultBoolean.True,
                .PrintHorizontalLines = DefaultBoolean.False
            })
        End Sub
        '...
    End Class
End Namespace

To take advantage of binding expressions, use the ReportGenerator.GenerateReport method with the useExpressionBindings Boolean parameter set to true. Binding expressions offer advanced capabilities for further customization of the generated report. See Data Binding Modes to learn more.

csharp
//...
XtraReport report = ReportGenerator.GenerateReport(gridView1, options, true);
vb
' ...
Dim report As XtraReport = ReportGenerator.GenerateReport(gridView1, options, True)

Open Report Designer. Preview and Print Generated Report

The following code displays the MyReport when a user clicks the “Preview” toolbar command. MyReport was generated and configured at design-time.

csharp
using DevExpress.XtraReports.UI;
using System;

//...
public partial class Form1 : DevExpress.XtraBars.Ribbon.RibbonForm {
    MyReport gridReport;
    public Form1() {
        InitializeComponent();
        gridReport = new MyReport();
    }
    void previewBarItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
        gridReport.ShowPreviewDialog();
    }
    void reportDesignerBarItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
        gridReport.ShowDesignerDialog();
    }
    void printBarItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
        gridReport.PrintDialog();
    }
}
vb
Imports DevExpress.XtraReports.UI
Imports System

'...
Partial Public Class Form1
    Inherits DevExpress.XtraBars.Ribbon.RibbonForm

    Private gridReport As MyReport
    Public Sub New()
        InitializeComponent()
        gridReport = New MyReport()
    End Sub
    Private Sub previewBarItem_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        gridReport.ShowPreviewDialog()
    End Sub
    Private Sub reportDesignerBarItem_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        gridReport.ShowDesignerDialog()
    End Sub
    Private Sub printBarItem_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        gridReport.PrintDialog()
    End Sub
End Class

The following image shows the result:

Scenarios of Using the Generated Report

The generated report is automatically added to the current project under the specified name and opened in the advanced Report Designer, which is fully integrated into the Visual Studio IDE. The report’s declaration can be found in the file with the same name as the report name.

The report designer provides rich creating, editing and publishing capabilities including the following.

  • Print, Print Preview, and Export actions
  • Modifying report element layouts
  • Modifying report element appearances
  • Applying data grouping, sorting and filtering
  • Adding totals
  • Adding page numbers and system information
  • etc.

See the Visual Studio Report Designer document for more information about report design-time customization.

After the report is generated and customized, you can show the report’s print preview to your end-users.

It is possible to delegate report customization to end-users. They can use the End-User Report Designer to manually modify the report, show a Print Preview, export the report, etc. For more information, refer to the Report Designer document.

The following code creates a new instance of the generated report class and opens it in the End-User Report Designer.

csharp
using DevExpress.XtraReports.UI;
//...

MyReport report = new MyReport();
ReportDesignTool designTool = new ReportDesignTool(report);
designTool.ShowDesignerDialog();
vb
Imports DevExpress.XtraReports.UI
'...

Dim report As New MyReport()
Dim designTool As New ReportDesignTool(report)
designTool.ShowDesignerDialog()

Limitations

The report generation engine does not support the following features: