Skip to main content

DevExpress v24.1 Update — Your Feedback Matters

Our What's New in v24.1 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Create a Report in Code

  • 3 minutes to read

The following steps describe how to create a report in code:

  1. Create an instance of the XtraReport class or implement an XtraReport class descendant.
  2. Assign a data source to the report: specify the DataSource and DataMember properties.
  3. Create bands and add them to the report’s Bands collection (the DetailBand is mandatory).
  4. Create report controls and add them to the report’s bands.
  5. Add styles to report elements.

After the report layout is complete, you have an instance of the XtraReport class descendant.

#Examples

The following examples illustrate how to generate specific report types in code:

The following topics describe the report object model and events:

#Next Steps

Once you create the XtraReport (or its descendant) instance, as described in the help topics in the previous section, you can perform the actions listed below. The XtraReport1 class mentioned in the code snippets is the XtraReport descendant.

#Preview

Use the following code in a WinForms application:

using DevExpress.XtraReports.UI;
// ...
ReportPrintTool printTool = new ReportPrintTool(new XtraReport1());
printTool.ShowPreview();

Review the following help topic for information specific to Web platforms: Document Viewer for Web.

#Edit

Use the following code in a WinForms application:

using DevExpress.XtraReports.UI;
// ...
ReportDesignTool designTool = new ReportDesignTool(new XtraReport1());
designTool.ShowDesigner();

Review the following help topic for information specific to Web platforms: End-User Report Designer for Web.

#Print

Use the following code in a WinForms application:

using DevExpress.XtraReports.UI;
// ...
ReportPrintTool printTool = new ReportPrintTool(new XtraReport1());
printTool.PrintDialog()

Review the following help topic for information specific to Web platforms: Printing and Export in Reporting Tools for Web.

#Export to PDF or Other Formats

using DevExpress.XtraReports.UI;
// ...
string pdfFilePath = @"C:\Temp\Report1.pdf";
XtraReport1 report = new XtraReport1();
report.ExportToPdf(pdfFilePath);

Review the following help topic for more information: Document Export Overview.

#Save in XML Format

using DevExpress.XtraReports.UI;
// ...
string reportFilePath = @"C:\Temp\Report1.repx";
string styleSheetFilePath = @"C:\Temp\ReportStyleSheet1.repss";
XtraReport1 report = new XtraReport1();
report.SaveLayoutToXml(reportFilePath);
report.StyleSheet.SaveXmlToFile(styleSheetFilePath);

Review the following help topic for more information: Save Report Layouts.

#Troubleshooting

If a runtime-generated report does not work as expected or you want to know how to implement a particular feature in code, follow these steps:

  1. Use the report’s SaveLayoutToXml method to save the generated report as a REPX file.
  2. Add a new report to your .NET Framework project, expand the report’s smart tag, and import the generated REPX file.

    Import a Report

  3. Specify the report’s data source and preview the changes.

  4. Modify the report layout.
  5. The XtraReport.Designer.cs or XtraReport.Designer.vb file contains code that generates the report. Use this code in your application to get a similar result.
See Also