Switching DataSources in ReportViewer in WinForms

匆匆过客 提交于 2019-12-07 17:18:40

问题


I have created a winform for the users to view view the many reports I am creating for them. I have a drop down list with the report name which triggers the appropriate fields to display the parameters. Once those are filled, they press Submit and the report appears. This works the first time they hit the screen. They can change the parameters and the ReportViewer works fine. Change to a different report, and the I get the following ReportViewer error:

An error occurred during local report processing.
An error has occurred during the report processing.
A data source instance has not been supplied for the data source "CgTempData_BusMaintenance".

As far as the process I use:

  1. I set reportName (string) the physical RDLC name.
  2. I set the dataSource (string) as the DataSource Name
  3. I fill a generic DataTable with the data for the report to run from.
  4. Make the ReportViewer visible
  5. Set the LocalReport.ReportPath = "Reports\\" = reportName;
  6. Clear the LocalReport.DataSources.Clear()
  7. Add the new LocalReport.DataSources.Add(new ReportDataSource(dataSource, dt));
  8. RefreshReport() on the ReportViewer.

Here is the portion of the code that setups up and displays the ReportViewer:

/// <summary>
/// Builds the report.
/// </summary>
private void BuildReport()
{
    DataTable dt = null;
    ReportingCG rcg = new ReportingCG();

    if (reportName == "GasUsedReport.rdlc")
    {
        dataSource = "CgTempData_FuelLog";
        CgTempData.FuelLogDataTable DtFuelLog = rcg.BuildFuelUsedTable(fromDate, toDate);
        dt = DtFuelLog;
    }
    else if (reportName == "InventoryCost.rdlc")
    {
        CgTempData.InventoryUsedDataTable DtInventory;
        dataSource = "CgTempData_InventoryUsed";
        DtInventory = rcg.BuildInventoryUsedTable(fromDate, toDate);
        dt = DtInventory;
    }
    else if (reportName == "VehicleMasterList.rdlc")
    {
        dataSource = "CgTempData_VehicleMaster";
        CgTempData.VehicleMasterDataTable DtVehicleMaster = rcg.BuildVehicleMasterTable();
        dt = DtVehicleMaster;
    }
    else if (reportName == "BusCosts.rdlc")
    {
        dataSource = "CgTempData_BusMaintenance";
        dt = rcg.BuildBusCostsTable(fromDate, toDate);
    }

    // Setup the DataSource
    this.reportViewer1.Visible = true;
    this.reportViewer1.LocalReport.ReportPath = "Reports\\" + reportName;
    this.reportViewer1.LocalReport.DataSources.Clear();
    this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource(dataSource, dt));
    this.reportViewer1.RefreshReport();
}

Any ideas how to remove all of the old remaining data? Do I dispose the object and recreate it?


回答1:


I figured it out. I needed to add: reportViewer1.Reset(); to the beginning of the method.



来源:https://stackoverflow.com/questions/2852369/switching-datasources-in-reportviewer-in-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!