Dynamically Load Embedded Resource Report Using Microsoft.Reporting.WinForms
Asked Answered
D

2

13

How does one dynamically load a new report from an embedded resource? I have created a reporting project that contains a report as an embedded resource. I added a second report file and use the following code to switch reports:

this.reportViewer1.LocalReport.ReportEmbeddedResource = "ReportsApplication2.Report2.rdlc";
this.reportViewer1.LocalReport.Refresh();
this.reportViewer1.RefreshReport();

When this code executes, the original report remains visible in the report viewer.

I have also tried using

LocalReport.LoadReportDefinition

but had the same result.

Delta answered 2/10, 2008 at 15:24 Comment(1)
When I try the application on my computer, it works, but the application does not work on another computerBatting
D
9

The answer: you have to call

<ReportViewer>.Reset();

prior to changing the value of ReportEmbeddedResource or calling LoadReportDefinition.

After you do so, you'll also have to call

<ReportViewer>.LocalReport.DataSources.Add( ... );

to re-establish the data sources.

Delta answered 3/10, 2008 at 14:27 Comment(1)
Just want to add a comment saying that you can run into errors with parameters not being defined unless you do this Reset as well.Soane
C
0

a better way to reference your reports is by using the default value of ReportEmbeddedResource, don't hard code it just change the name of the report.

//choose which report to load
        string reportEmbeddedResource = this.orderReportViewer.LocalReport.ReportEmbeddedResource;
        //remove the extention .rdlc
        reportEmbeddedResource = reportEmbeddedResource.Remove(reportEmbeddedResource.LastIndexOf('.'));
        //remove name of current report ex: .invoice.rdlc
        reportEmbeddedResource = reportEmbeddedResource.Remove(reportEmbeddedResource.LastIndexOf('.'));
        //clear current reportEmbeddedResource
        this.orderReportViewer.Reset();
        if (_retailReceip)
        {
            this.orderReportViewer.LocalReport.ReportEmbeddedResource = reportEmbeddedResource + ".PrintReceipt.rdlc";
        }
        else
        {
            this.orderReportViewer.LocalReport.ReportEmbeddedResource = reportEmbeddedResource +  ".PrintOrder.rdlc";
        }
Condenser answered 21/3, 2022 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.