xtrareports-403671-feature-guide-to-devexpress-reports-reporting-api-create-reports-in-code-create-a-simple-data-bound-report.md
The following code sample creates a new object data source, creates a report with the XRLabel control at runtime, and binds the label to data. The ReportPrintTool displays the created report to the user.
For information on supported data sources, review the following article: Bind Reports to Data.
using System;
using DevExpress.XtraReports.UI;
namespace CreateSimpleReportInCode {
class Program {
[STAThread]
static void Main(string[] args) {
XtraReport report = ReportCreator.CreateReport();
ReportPrintTool printTool = new ReportPrintTool(report);
printTool.ShowRibbonPreviewDialog();
}
}
}
Imports System
Imports DevExpress.XtraReports.UI
Namespace CreateSimpleReportInCode
Friend Class Program
<STAThread>
Shared Sub Main(ByVal args() As String)
Dim report As XtraReport = ReportCreator.CreateReport()
Dim printTool As New ReportPrintTool(report)
printTool.ShowRibbonPreviewDialog()
End Sub
End Class
End Namespace
using System.Collections.Generic;
using System.Drawing;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
// ...
public class ReportCreator {
class Employee {
public string Name { get; set; }
}
public static XtraReport CreateReport() {
XtraReport report = new XtraReport() {
DataSource = CreateDataSource(),
StyleSheet = {
new XRControlStyle() { Name = "Title", Font = new Font("Tahoma", 20f, FontStyle.Bold) },
new XRControlStyle() { Name = "Normal", Font = new Font("Tahoma", 10f), Padding = new PaddingInfo(2, 2, 0, 0) },
}
};
var reportHeaderBand = CreateReportHeader("List of employees");
var detailBand = CreateDetail("[Name]");
report.Bands.AddRange(new Band[] { reportHeaderBand, detailBand });
return report;
}
static List<Employee> CreateDataSource() {
return new List<Employee>() {
new Employee() { Name = "Nancy Davolio" },
new Employee() { Name = "Andrew Fuller" },
new Employee() { Name = "Janet Leverling" },
new Employee() { Name = "Margaret Peacock" },
new Employee() { Name = "Steven Buchanan" },
new Employee() { Name = "Michael Suyama" },
new Employee() { Name = "Robert King" },
new Employee() { Name = "Laura Callahan" },
new Employee() { Name = "Anne Dodsworth" }
};
}
static ReportHeaderBand CreateReportHeader(string title) {
ReportHeaderBand reportHeaderBand = new ReportHeaderBand() {
HeightF = 50
};
XRLabel titleLabel = new XRLabel() {
Text = title,
BoundsF = new RectangleF(0, 0, 300, 30),
StyleName = "Title"
};
reportHeaderBand.Controls.Add(titleLabel);
return reportHeaderBand;
}
static DetailBand CreateDetail(string expression) {
DetailBand detailBand = new DetailBand() {
HeightF = 25
};
XRLabel detailLabel = new XRLabel() {
ExpressionBindings = { new ExpressionBinding("Text", expression) },
BoundsF = new RectangleF(0, 0, 300, 20),
StyleName = "Normal"
};
detailBand.Controls.Add(detailLabel);
return detailBand;
}
}
Imports System.Collections.Generic
Imports System.Drawing
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
' ...
Public Class ReportCreator
Private Class Employee
Public Property Name() As String
End Class
Public Shared Function CreateReport() As XtraReport
Dim report As New XtraReport() With {
.DataSource = CreateDataSource(), .StyleSheet = {
New XRControlStyle() With {.Name = "Title", .Font = New Font("Tahoma", 20F, FontStyle.Bold)},
New XRControlStyle() With {.Name = "Normal", .Font = New Font("Tahoma", 10F), .Padding = New PaddingInfo(2, 2, 0, 0)}
}
}
Dim reportHeaderBand = CreateReportHeader("List of employees")
Dim detailBand = CreateDetail("[Name]")
report.Bands.AddRange(New Band() { reportHeaderBand, detailBand })
Return report
End Function
Private Shared Function CreateDataSource() As List(Of Employee)
Return New List(Of Employee)() From {
New Employee() With {.Name = "Nancy Davolio"},
New Employee() With {.Name = "Andrew Fuller"},
New Employee() With {.Name = "Janet Leverling"},
New Employee() With {.Name = "Margaret Peacock"},
New Employee() With {.Name = "Steven Buchanan"},
New Employee() With {.Name = "Michael Suyama"},
New Employee() With {.Name = "Robert King"},
New Employee() With {.Name = "Laura Callahan"},
New Employee() With {.Name = "Anne Dodsworth"}
}
End Function
Private Shared Function CreateReportHeader(ByVal title As String) As ReportHeaderBand
Dim reportHeaderBand As New ReportHeaderBand() With {.HeightF = 50}
Dim titleLabel As New XRLabel() With {.Text = title, .BoundsF = New RectangleF(0, 0, 300, 30), .StyleName = "Title"}
reportHeaderBand.Controls.Add(titleLabel)
Return reportHeaderBand
End Function
Private Shared Function CreateDetail(ByVal expression As String) As DetailBand
Dim detailBand As New DetailBand() With {.HeightF = 25}
Dim detailLabel As New XRLabel() With {.ExpressionBindings = { New ExpressionBinding("Text", expression) }, .BoundsF = New RectangleF(0, 0, 300, 20), .StyleName = "Normal"}
detailBand.Controls.Add(detailLabel)
Return detailBand
End Function
End Class