[]
The Configuring Resources Storage topic explains how to resolve reports from static storage locations, such as file systems or databases. However, in some scenarios, you may need to initialize report objects (like PageReport or SectionReport) dynamically at runtime. Runtime report initialization covers such issues as:
Supplying dynamic data
Setting parameters
Loading reports from different file formats
Applying caching strategies to improve performance
The RuntimeReportInitializer is responsible for locating and loading reports at runtime. Below is a basic example of registering the initializer from a reports directory and enabling it in a viewer.
var reportsDir = new DirectoryInfo(Path.Combine(app.Environment.ContentRootPath, "Reports"));
var reportInitializer = new RuntimeReportInitializer(reportsDir.FullName);
app.UseReportViewer(config =>
{
config.UseRuntimeReportInitializer(reportInitializer);
});When working with RDLX-based reports, you can provide data dynamically at runtime by using the LocateDataSource event. The code sample below demonstrates how to attach a data source object programmatically. This approach avoids hardcoding data sources in the report design, allowing flexibility depending on runtime conditions.
public class RuntimeReportInitializer : IRuntimeReportInitializer
{
private string _reportPath;
public RuntimeReportInitializer(string reportPath)
{
_reportPath = reportPath;
}
public object GetReport(string reportId)
{
var reportPath = Path.Combine(_reportPath, reportId);
var report = new PageReport(new FileInfo(reportPath));
report.Document.LocateDataSource+= (s, e) =>
{
e.Data = <Data Object>
};
return report;
}
}using GrapeCity.ActiveReports.SectionReportModel;
using System.Data;
public void LoadSectionReportData()
{
var report = new SectionReport();
// Example: populate DataTable dynamically
DataTable dt = new DataTable("Products");
dt.Columns.Add("ProductName", typeof(string));
dt.Columns.Add("Price", typeof(decimal));
dt.Rows.Add("Widget A", 19.99m);
dt.Rows.Add("Widget B", 29.99m);
// Assign to report's DataSource
report.DataSource = dt;
// Run or preview
report.Run();
}You can assign or modify parameters programmatically when the report is loaded. This allows you to control filtering, formatting, or any report logic based on input from users, applications, or other services.
public class RuntimeReportInitializer : IRuntimeReportInitializer
{
private string _reportPath;
public RuntimeReportInitializer(string reportPath)
{
_reportPath = reportPath;
}
public object GetReport(string reportId)
{
var reportPath = Path.Combine(_reportPath, reportId);
var report = new PageReport(new FileInfo(reportPath));
report.Document.Parameters["PONumber"].Values.Add(
new GrapeCity.ActiveReports.Core.Rendering.ReportParameters.ParameterValue
{
Value = "PO1001"
}
);
report.Document.LocateDataSource += (s, e) =>
{
e.Data = purchaseOrders;
};
return report;
}
}RDF files are report document files created after running a report. You can load and display them without re-running the data queries.
using GrapeCity.ActiveReports.Document.Section;
using System.IO;
public void LoadRdfReport()
{
var doc = new SectionDocument();
using (var fs = new FileStream("Invoice.rdf", FileMode.Open))
{
doc.Load(fs);
}
// Optionally view in a viewer control
var viewer = new GrapeCity.ActiveReports.Viewer.Win.Viewer();
viewer.LoadDocument(doc);
}.rdlx-snap files are saved snapshots of RDLX reports containing both the definition and rendered data.
using GrapeCity.ActiveReports.Document.Page;
using System.IO;
public void LoadRdlxSnapReport()
{
var doc = new PageDocument();
using (var fs = new FileStream("SalesSummary.rdlx-snap", FileMode.Open))
{
doc.Load(fs);
}
// Display using the viewer
var viewer = new GrapeCity.ActiveReports.Viewer.Win.Viewer();
viewer.LoadDocument(doc);
}