Ticket T1270730
Visible to All Users

Change data Source of report with code windows form C#

created 3 months ago

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#

Answers approved by DevExpress Support

created 3 months ago

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

    Show previous comments (1)
    Alessandro (DevExpress Support) 3 months ago

      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

          Don't worry I solved it in the constructor of the report. Best regards,

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.