Hello, I need to change the data source of my report with code so that can I use the main data source of the application. I am using WinForm with C#
Change data Source of report with code windows form C#
Answers approved by DevExpress Support
Hi,
Please use the DataSourceManager API to complete this task (e.g., see Replace a Report Data Source). I hope you find this information helpful.
Regards,
Alessandro
Hi,
You can use the following logic:
C#var report = new XtraReport1();
var sqlDataSource = new SqlDataSource("NWindConnectionString");
var query = SelectQueryFluentBuilder
.AddTable("Products")
.SelectAllColumns()
.Build("Products");
sqlDataSource.Queries.Add(query);
sqlDataSource.RebuildResultSchema();
DataSourceManager.ReplaceDataSource(report, report.DataSource, sqlDataSource);
report.DataMember = sqlDataSource.Queries[0].Name;
var printTool = new ReportPrintTool(report);
printTool.ShowPreviewDialog();
In addition, please make sure that the connection string name ("NWindConnectionString" in this example) is defined in the application configuration file (app.config
or appsettings.json
for .NET Core apps) to avoid the "No connection has been specified" error. Attached is a standalone sample project and the screencast, which illustrate the described solution in action. If this information does not help, please modify my sample project to illustrate the issue so that we can reproduce and debug it locally.
Hello Alessandro Great!, it worked for the header and the detail of the report, but what about if I want to get the company information for the header of the report from another datasourse, can I use two data sources in code for that matter? Thanks for your answer