Back to Devexpress

Generate a Grid-Based Report

wpf-117300-controls-and-libraries-data-grid-printing-and-exporting-grid-based-report-generation.md

latest6.1 KB
Original Source

Generate a Grid-Based Report

  • Dec 08, 2023
  • 2 minutes to read

You can generate a Grid-based Report with the same data, appearance, and layout as in the displayed GridControl. End users can customize a Report appearance.

Note

You can generate a Grid-based Report only from the TableView.

Generate a Report at Runtime

The following example demonstrates how to initialize and open the Report Designer in code. To use this example in your project, add references to the DevExpress.Xpf.ReportDesigner.v25.2 and DevExpress.XtraReports.v25.2 assemblies.

csharp
using System.Windows;
using System.Collections.ObjectModel;
using DevExpress.XtraReports.UI;
using DevExpress.Xpf.Grid.Printing;
using DevExpress.XtraExport.Helpers;
using DevExpress.Xpf.Reports.UserDesigner;

namespace DXGridSample {
    public partial class MainWindow : Window {
        ...
        void BarButtonItem_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e) {
            ShowDesigner(tableView);
        }
        // Initializes and runs a Report Designer.
        public static void ShowDesigner(IGridViewFactory<ColumnWrapper, RowBaseWrapper> factory) {
            var report = new XtraReport();
            ReportGenerationExtensions<ColumnWrapper, RowBaseWrapper>.Generate(report, factory);
            ReportDesigner reportDesigner = new ReportDesigner();
            reportDesigner.Loaded += (s, e) => {
                reportDesigner.OpenDocument(report);
            };
            reportDesigner.ShowWindow(factory as FrameworkElement);
        }
    }
}
vb
Imports System.Windows
Imports System.Collections.ObjectModel
Imports DevExpress.XtraReports.UI
Imports DevExpress.Xpf.Grid.Printing
Imports DevExpress.XtraExport.Helpers
Imports DevExpress.Xpf.Reports.UserDesigner

Namespace DXGridSample
    Partial Public Class MainWindow
        Inherits Window
        ...
        Sub BarButtonItem_ItemClick(ByVal sender As Object, ByVal e As DevExpress.Xpf.Bars.ItemClickEventArgs)
            ShowDesigner(tableView)
        End Sub
        ' Initializes and runs a Report Designer.
        Public Shared Sub ShowDesigner(ByVal factory As IGridViewFactory(Of ColumnWrapper, RowBaseWrapper))
            Dim report = New XtraReport()
            ReportGenerationExtensions(Of ColumnWrapper, RowBaseWrapper).Generate(report, factory)
            Dim reportDesigner As New ReportDesigner()
            AddHandler reportDesigner.Loaded, Sub(s, e) reportDesigner.OpenDocument(report)
            reportDesigner.ShowWindow(TryCast(factory, FrameworkElement))
        End Sub
    End Class
End Namespace

Use Behaviors to Generate a Report

To initialize and open the Report Designer in XAML:

  1. Add reference to the DevExpress.Xpf.ReportDesigner.v25.2 assembly.
  2. Attach the GridReportManagerService to a TableView.
  3. Attach the ReportManagerBehavior to a ribbon or bar item.
  4. Bind the ReportManagerBehavior .Service property to GridReportManagerService.

View Example: Use the GridReportManagerService to Implement the Report Management Functionality

xaml
<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" 
    xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars" 
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    xmlns:dxrudex="http://schemas.devexpress.com/winfx/2008/xaml/reports/userdesignerextensions" 
    x:Class="DXGridSample.MainWindow"
    Title="MainWindow" 
    Height="350" Width="525">
    <DockPanel>
        <dxb:ToolBarControl DockPanel.Dock="Top" >
            <dxb:BarSplitButtonItem Glyph="{dx:DXImage Image=Print_16x16.png}">
                <dxmvvm:Interaction.Behaviors>
                    <dxrudex:ReportManagerBehavior Service="{Binding ElementName=reportManagerService}"/>
                </dxmvvm:Interaction.Behaviors>
            </dxb:BarSplitButtonItem>
        </dxb:ToolBarControl>
        <dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew">
            <dxg:GridControl.View>
                <dxg:TableView x:Name="view">
                    <dxmvvm:Interaction.Behaviors>
                        <dxrudex:GridReportManagerService x:Name="reportManagerService" />
                    </dxmvvm:Interaction.Behaviors>
                </dxg:TableView>
            </dxg:GridControl.View>
        </dxg:GridControl>
    </DockPanel>
</Window>

Limitations

  • If the GridControl works in master-detail mode, only the master View is displayed in the Report Designer. Users can manually add detail rows to the Report.
  • The merged grid cells are exported as separate cells.
  • Custom grouping, sorting, and summaries are not applied to the Report.
  • The report displays summaries from the top summary panel below data rows.

See Also

Print Data