Example T356256
Visible to All Users

Reporting for WPF - How to create a report in code

This example illustrates the process of creating a report in code in a WPF application.

The following steps are essential to create a report layout:

  1. Create a report class instance and bind it to data.
  2. Add bands to the report.
  3. Add controls to the created bands and bind them to data.

After the report layout is complete, you can generate the report document and display it in the Print Preview.

Report in WPF

Starting with v.17.2, the report uses binding expressions to bind controls to data. You can switch to the legacy binding mode by setting the UserDesignerOptions.DataBindingMode property to Bindings at the application start.

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

RuntimeReportsApplication/App.xaml.cs
C#
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Threading.Tasks; using System.Windows; namespace RuntimeReportsApplication { /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { //Uncomment this line to switch to the legacy binding mode. //DevExpress.XtraReports.Configuration.Settings.Default.UserDesignerOptions.DataBindingMode = // DevExpress.XtraReports.UI.DataBindingMode.Bindings; base.OnStartup(e); } } }
RuntimeReportsApplication/Application.xaml.vb
Visual Basic
Imports System Imports System.Collections.Generic Imports System.Configuration Imports System.Data Imports System.Linq Imports System.Threading.Tasks Imports System.Windows Namespace RuntimeReportsApplication ''' <summary> ''' Interaction logic for App.xaml ''' </summary> Partial Public Class App Inherits Application Protected Overrides Sub OnStartup(ByVal e As StartupEventArgs) 'Uncomment this line to switch to the legacy binding mode. 'DevExpress.XtraReports.Configuration.Settings.Default.UserDesignerOptions.DataBindingMode = ' DevExpress.XtraReports.UI.DataBindingMode.Bindings; MyBase.OnStartup(e) End Sub End Class End Namespace
RuntimeReportsApplication/MainWindow.xaml.cs(vb)
C#
#region #Reference using DevExpress.DataAccess.ConnectionParameters; using DevExpress.DataAccess.Sql; using DevExpress.Xpf.Printing; using DevExpress.XtraPrinting; using DevExpress.XtraReports.Configuration; using DevExpress.XtraReports.UI; using System.Drawing; using System.Windows; // ... #endregion #Reference namespace RuntimeReportsApplication { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } #region #CreateReport public XtraReport CreateReport() { // Create an empty report. XtraReport report = new XtraReport(); // Bind the report to a data source. BindToData(report); // Create a master report. CreateReportHeader(report, "Products by Categories"); CreateDetail(report); // Create a detail report. SqlDataSource ds = report.DataSource as SqlDataSource; CreateDetailReport(report, ds.Queries[0].Name + "." + ds.Relations[0].Name); return report; } #endregion #CreateReport #region #BindToData private void BindToData(XtraReport report) { // Create a data source. Access97ConnectionParameters connectionParameters = new Access97ConnectionParameters("../../nwind.mdb", "", ""); DevExpress.DataAccess.Sql.SqlDataSource ds = new DevExpress.DataAccess.Sql.SqlDataSource(connectionParameters); // Create an SQL query to access the master table. CustomSqlQuery queryCategories = new CustomSqlQuery(); queryCategories.Name = "queryCategories"; queryCategories.Sql = "SELECT * FROM Categories"; // Create an SQL query to access the detail table. CustomSqlQuery queryProducts = new CustomSqlQuery(); queryProducts.Name = "queryProducts"; queryProducts.Sql = "SELECT * FROM Products"; // Add the queries to the data source collection. ds.Queries.AddRange(new SqlQuery[] { queryCategories, queryProducts }); // Create a master-detail relation between the queries. ds.Relations.Add("queryCategories", "queryProducts", "CategoryID", "CategoryID"); // Assign the data source to the report. report.DataSource = ds; report.DataMember = "queryCategories"; } #endregion #BindToData #region #CreateMasterReport private void CreateReportHeader(XtraReport report, string caption) { // Create a report title. XRLabel label = new XRLabel(); label.Font = new Font("Tahoma", 12, System.Drawing.FontStyle.Bold); label.Text = caption; label.WidthF = 300F; // Create a report header and add the title to it. ReportHeaderBand reportHeader = new ReportHeaderBand(); report.Bands.Add(reportHeader); reportHeader.Controls.Add(label); reportHeader.HeightF = label.HeightF; } private void CreateDetail(XtraReport report) { // Create a new label with the required settings. bound to the CategoryName data field. XRLabel labelDetail = new XRLabel(); labelDetail.Font = new Font("Tahoma", 10, System.Drawing.FontStyle.Bold); labelDetail.WidthF = 300F; // Bind the label to the CategoryName data field depending on the report's data binding mode. if (Settings.Default.UserDesignerOptions.DataBindingMode == DataBindingMode.Bindings) labelDetail.DataBindings.Add("Text", report.DataSource, "queryCategories.CategoryName", "Category: {0}"); else labelDetail.ExpressionBindings.Add( new ExpressionBinding("BeforePrint", "Text", "'Category: ' + [CategoryName]")); // Create a detail band and display the category name in it. DetailBand detailBand = new DetailBand(); detailBand.Height = labelDetail.Height; detailBand.KeepTogetherWithDetailReports = true; report.Bands.Add(detailBand); labelDetail.TopF = detailBand.LocationFloat.Y + 20F; detailBand.Controls.Add(labelDetail); } #endregion #CreateMasterReport #region #CreateDetailReport private void CreateDetailReport(XtraReport report, string dataMember) { // Create a detail report band and bind it to data. DetailReportBand detailReportBand = new DetailReportBand(); report.Bands.Add(detailReportBand); detailReportBand.DataSource = report.DataSource; detailReportBand.DataMember = dataMember; // Add a header to the detail report. ReportHeaderBand detailReportHeader = new ReportHeaderBand(); detailReportBand.Bands.Add(detailReportHeader); XRTable tableHeader = new XRTable(); tableHeader.BeginInit(); tableHeader.Rows.Add(new XRTableRow()); tableHeader.Borders = BorderSide.All; tableHeader.BorderColor = Color.DarkGray; tableHeader.Font = new Font("Tahoma", 10, System.Drawing.FontStyle.Bold); tableHeader.Padding = 10; tableHeader.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft; XRTableCell cellHeader1 = new XRTableCell(); cellHeader1.Text = "Product Name"; XRTableCell cellHeader2 = new XRTableCell(); cellHeader2.Text = "Unit Price"; cellHeader2.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleRight; tableHeader.Rows[0].Cells.AddRange(new XRTableCell[] { cellHeader1, cellHeader2 }); detailReportHeader.Height = tableHeader.Height; detailReportHeader.Controls.Add(tableHeader); // Adjust the table width. tableHeader.BeforePrint += tableHeader_BeforePrint; tableHeader.EndInit(); // Create a detail band. XRTable tableDetail = new XRTable(); tableDetail.BeginInit(); tableDetail.Rows.Add(new XRTableRow()); tableDetail.Borders = BorderSide.Left | BorderSide.Right | BorderSide.Bottom; tableDetail.BorderColor = Color.DarkGray; tableDetail.Font = new Font("Tahoma", 10); tableDetail.Padding = 10; tableDetail.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft; XRTableCell cellDetail1 = new XRTableCell(); XRTableCell cellDetail2 = new XRTableCell(); cellDetail2.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleRight; if (Settings.Default.UserDesignerOptions.DataBindingMode == DataBindingMode.Bindings) { cellDetail1.DataBindings.Add("Text", report.DataSource, dataMember + ".ProductName"); cellDetail2.DataBindings.Add("Text", report.DataSource, dataMember + ".UnitPrice", "{0:$0.00}"); } else { cellDetail1.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Text", "[ProductName]")); cellDetail2.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Text", "FormatString('{0:$0.00}', [UnitPrice])")); } tableDetail.Rows[0].Cells.AddRange(new XRTableCell[] { cellDetail1, cellDetail2 }); DetailBand detailBand = new DetailBand(); detailBand.Height = tableDetail.Height; detailReportBand.Bands.Add(detailBand); detailBand.Controls.Add(tableDetail); // Adjust the table width. tableDetail.BeforePrint += tableDetail_BeforePrint; tableDetail.EndInit(); // Create and assign different odd and even styles. XRControlStyle oddStyle = new XRControlStyle(); XRControlStyle evenStyle = new XRControlStyle(); oddStyle.BackColor = Color.WhiteSmoke; oddStyle.StyleUsing.UseBackColor = true; oddStyle.Name = "OddStyle"; evenStyle.BackColor = Color.White; evenStyle.StyleUsing.UseBackColor = true; evenStyle.Name = "EvenStyle"; report.StyleSheet.AddRange(new XRControlStyle[] { oddStyle, evenStyle }); tableDetail.OddStyleName = "OddStyle"; tableDetail.EvenStyleName = "EvenStyle"; } private void AdjustTableWidth(XRTable table) { XtraReport report = table.RootReport; table.WidthF = report.PageWidth - report.Margins.Left - report.Margins.Right; } void tableHeader_BeforePrint(object sender, System.ComponentModel.CancelEventArgs e) { AdjustTableWidth(sender as XRTable); } void tableDetail_BeforePrint(object sender, System.ComponentModel.CancelEventArgs e) { AdjustTableWidth(sender as XRTable); } #endregion #CreateDetailReport private void Window_Loaded(object sender, RoutedEventArgs e) { XtraReport report = CreateReport(); PrintHelper.ShowRibbonPrintPreview(this, report); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.