Create a Report in Code
- 3 minutes to read
The following steps describe how to create a report in code:
- Create an instance of the XtraReport class or implement an XtraReport class descendant.
- Assign a data source to the report: specify the DataSource and DataMember properties.
- Create bands and add them to the report’s Bands collection (the DetailBand is mandatory).
- Create report controls and add them to the report’s bands.
- 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:
- Simple Static Report
- Simple Data-Bound Report
- Table Report
- Table Report with Dynamic Columns
- Master-Detail Report
- Cross-Tab Report
- Report with a Subreport
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.
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:
- Use the report’s SaveLayoutToXml method to save the generated report as a REPX file.
Add a new report to your .NET Framework project, expand the report’s smart tag, and import the generated REPX file.
Specify the report’s data source and preview the changes.
- Modify the report layout.
- The
XtraReport.Designer.cs
orXtraReport.Designer.vb
file contains code that generates the report. Use this code in your application to get a similar result.