xtrareports-403989-feature-guide-to-devexpress-reports-bind-reports-to-data-manage-datasources-at-runtime.md
When you create a reporting application, you might need to modify or configure data sources for your reports at runtime. Such runtime configuration/modification can be required in the following cases:
The listed and other similar tasks can be solved with the use of the DataSourceManager class. This static class contains five methods that allow you to do basic operations with report data sources:
GetDataSourcesReturns all report data sources.AddDataSourcesAdds the specified data sources to a report.ReplaceDataSourceReplaces a report’s current data source with the specified data source.GetDataSourceAssignablesReturns a report and its elements (subreports, controls, bands, parameters) to which a data source can be assigned.GetDataSourceAssignablesByDataSourceReturns a report and its elements (subreports, controls, bands, parameters) to which the specified data source is assigned.
The sections below contain two examples on how to use the DataSourceManager class methods. For more information on typical tasks related to runtime configuration/modification of report data sources and an explanation on how to solve these tasks, refer to the description of the DataSourceManager class and its methods.
The following code sample adds two JSON data sources to a report:
using DevExpress.DataAccess.Json;
using DevExpress.XtraReports;
//...
var report = new XtraReport1();
var jsonDataSource1 = new JsonDataSource { /* ... */ };
var jsonDataSource2 = new JsonDataSource { /* ... */ };
DataSourceManager.AddDataSources(report, jsonDataSource1, jsonDataSource2);
Imports DevExpress.DataAccess.Json
Imports DevExpress.XtraReports
'...
Private report = New XtraReport1()
Private jsonDataSource1 = New JsonDataSource()
Private jsonDataSource2 = New JsonDataSource()
DataSourceManager.AddDataSources(report, jsonDataSource1, jsonDataSource2)
The following code template shows how to use the GetDataSources method to retrieve all data sources of the SqlDataSource type and update their query parameters.
using DevExpress.DataAccess.Sql;
using DevExpress.XtraReports;
//...
var report = new XtraReport1();
var sqlDataSources = DataSourceManager.GetDataSources<SqlDataSource>(
report: report,
includeSubReports: true
);
foreach (var sqlDataSource in sqlDataSources) {
foreach (var query in sqlDataSource.Queries) {
query.Parameters["paramName"].Value = 32;
}
}
Imports DevExpress.DataAccess.Sql
Imports DevExpress.XtraReports
'...
Private report = New XtraReport1()
Private sqlDataSources = DataSourceManager.GetDataSources(Of SqlDataSource)(report:= report, includeSubReports:= True)
For Each sqlDataSource In sqlDataSources
For Each query In sqlDataSource.Queries
query.Parameters("paramName").Value = 32
Next query
Next sqlDataSource