Back to Devexpress

Bind a Report to a CSV File (Runtime Sample)

xtrareports-114912-feature-guide-to-devexpress-reports-bind-reports-to-data-excel-file-bind-a-report-to-a-csv-file-runtime-sample.md

latest4.4 KB
Original Source

Bind a Report to a CSV File (Runtime Sample)

  • Feb 18, 2026
  • 3 minutes to read

This example demonstrates how to bind a report to a file that contains data in CSV format, and specify the report layout at runtime.

View Example: Reporting for WinForms - Bind a Report to a CSV File at Runtime

csharp
using DevExpress.DataAccess.Excel;
using DevExpress.XtraReports.Configuration;
using DevExpress.XtraReports.UI;
// ...
// Create an empty report.
XtraReport report = new XtraReport();

// Create a new Excel data source.
ExcelDataSource excelDataSource = new ExcelDataSource {
    FileName = "..//..//Northwind.csv"
};

// Specify import settings.
CsvSourceOptions csvSourceOptions = new CsvSourceOptions {
    DetectEncoding = true,
    DetectNewlineType = true,
    DetectValueSeparator = true
};
excelDataSource.SourceOptions = csvSourceOptions;

// Define the data source schema.
FieldInfo fieldCategoryID = new FieldInfo { Name = "CategoryID", Type = typeof(double), Selected = false };
FieldInfo fieldCategoryName = new FieldInfo { Name = "CategoryName", Type = typeof(string) };
FieldInfo fieldDescription = new FieldInfo { Name = "Description", Type = typeof(string) };
// Add the created fields to the data source schema in the order that matches the column order in the source file.
excelDataSource.Schema.AddRange(new FieldInfo[] { fieldCategoryID, fieldCategoryName, fieldDescription });

// Assign the data source to the report.
report.DataSource = excelDataSource;

// Add a detail band to the report.
DetailBand detailBand = new DetailBand {
    Height = 50
};
report.Bands.Add(detailBand);

// Create a new label.
XRLabel label = new XRLabel();
// Specify the label's binding depending on the data binding mode.
if (Settings.Default.UserDesignerOptions.DataBindingMode == DataBindingMode.Bindings)
    label.DataBindings.Add("Text", null, "CategoryName");
else label.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Text", "[CategoryName]"));
// Add the label to the detail band.
detailBand.Controls.Add(label);

// Show the report's print preview.
report.ShowPreview();
vb
Imports DevExpress.DataAccess.Excel
Imports DevExpress.XtraReports.Configuration
Imports DevExpress.XtraReports.UI
' ...
' Create an empty report.
Dim report As XtraReport = New XtraReport()
' Create a new Excel data source.
Dim excelDataSource As ExcelDataSource = New ExcelDataSource With {.FileName = "..//..//Northwind.csv"}
' Specify import settings.
Dim csvSourceOptions As CsvSourceOptions = New CsvSourceOptions With {.DetectEncoding = True, .DetectNewlineType = True, .DetectValueSeparator = True}
excelDataSource.SourceOptions = csvSourceOptions
' Define the data source schema.
Dim fieldCategoryID As FieldInfo = New FieldInfo With {.Name = "CategoryID", .Type = GetType(Double), .Selected = False}
Dim fieldCategoryName As FieldInfo = New FieldInfo With {.Name = "CategoryName", .Type = GetType(String)}
Dim fieldDescription As FieldInfo = New FieldInfo With {.Name = "Description", .Type = GetType(String)}
' Add the created fields to the data source schema in the order that matches the column order in the source file.
excelDataSource.Schema.AddRange(New FieldInfo() {fieldCategoryID, fieldCategoryName, fieldDescription})
' Assign the data source to the report.
report.DataSource = excelDataSource
' Add a detail band to the report.
Dim detailBand As DetailBand = New DetailBand With {.Height = 50}
report.Bands.Add(detailBand)
' Create a new label.
Dim label As XRLabel = New XRLabel()
' Specify the label's binding depending on the data binding mode.
If Settings.Default.UserDesignerOptions.DataBindingMode = DataBindingMode.Bindings Then
    label.DataBindings.Add("Text", Nothing, "CategoryName")
Else
    label.ExpressionBindings.Add(New ExpressionBinding("BeforePrint", "Text", "[CategoryName]"))
End If

' Add the label to the detail band.
detailBand.Controls.Add(label)
' Show the report's print preview.
report.ShowPreview()

See Also

Bind a Report to a CSV File

Bind a Report to an Excel Workbook (Runtime Sample)