Grid-Based Report Generation
- 2 minutes to read
With grid-based report generation you can get a report with the same data, design, and layout as your GridControl currently displays. Your end-users can customize an automatically built report design without having to build a report from scratch.
Note
The grid-based report generation feature works only with the TableView.
You can enable grid-based report generation at runtime (in code) or using behaviors without additional coding (in XAML).
Enabling Grid-Based Report Generation at Runtime
The example below demonstrates how to initialize and open the Report Designer at runtime.
<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"
x:Class="DXGridSample.MainWindow"
Title="MainWindow"
Height="350"
Width="525">
<DockPanel>
<dxb:ToolBarControl DockPanel.Dock="Top">
<dxb:BarButtonItem Content="Open Report Designer" ItemClick="BarButtonItem_ItemClick"/>
</dxb:ToolBarControl>
<dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew">
<dxg:GridControl.View>
<dxg:TableView x:Name="tableView" ShowTotalSummary="True" />
</dxg:GridControl.View>
</dxg:GridControl>
</DockPanel>
</Window>
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 {
...
private 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);
var reportDesigner = new ReportDesigner();
reportDesigner.Loaded += (s, e) => {
reportDesigner.OpenDocument(report);
};
reportDesigner.ShowWindow(factory as FrameworkElement);
}
}
}
The image below illustrates the result.
Enabling Grid-Based Report Generation Using Behaviors
To enable grid-based reporting using behaviors, attach the GridReportManagerService to a TableView, then attach ReportManagerBehavior to a ribbon or bar item and bind its ReportManagerBehavior.Service property to GridReportManagerService.
The code sample below demonstrates how to enable grid-based report generation in XAML only, without any additional coding.
<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" ShowTotalSummary="True">
<dxmvvm:Interaction.Behaviors>
<dxrudex:GridReportManagerService x:Name="reportManagerService" />
</dxmvvm:Interaction.Behaviors>
</dxg:TableView>
</dxg:GridControl.View>
</dxg:GridControl>
</DockPanel>
</Window>
The image below illustrates the result.
Limitations
- If the GridControl functions in master-detail mode, only the master view is displayed within the report designer by default. Note that your end-users can manually add detail rows to report.
- The merged grid cells are exported as separate cells.
- Custom grouping, custom sorting, and custom summaries will not be applied to report design.