Wednesday, March 3, 2010

Windows ReportViewer - Change rdlc report files during runtime

Problem

I had faced the problem of assigning different rdlc report files to a single Reportviewer. It always shows the report which opened first.

Solution
  1. Clear the datasources of the LocalReport or ServerReport in ReportViewer
  2. Reset the ReportViewer control in the beggining of the function.
  3. Do assign the datasources Name and Value. The Datasources name from the dataset as string and value.
  4. Finally add the datasource to the reportviwer datasource in localreport or serverreport.
Sample c# code snippet to solve this problem

this.reportViewer1.Reset();
this.reportViewer1.LocalReport.DataSources.Clear();
reportDataSource.Name = "DataSet_usp_EmpFoodCouponMonthly";
reportDataSource.Value = this.DataSet.usp_EmpFoodCouponMonthly;
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
this.reportViewer1.RefreshReport();