在WinForms中切换ReportViewer中的DataSources

时间:2010-05-17 20:09:18

标签: c# winforms reportviewer datasource

我为用户创建了一个winform,以查看我为他们创建的许多报告。我有一个带有报告名称的下拉列表,它触发相应的字段以显示参数。一旦填写完毕,他们按提交并显示报告。这是他们第一次点击屏幕时的工作原理。他们可以更改参数,ReportViewer可以正常工作。更改为其他报告,我收到以下ReportViewer错误:

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".

就我使用的流程而言:

  1. 我将reportName(字符串)设置为物理RDLC名称。
  2. 我将dataSource(字符串)设置为DataSource名称
  3. 我在通用DataTable中填写要运行的报告数据。
  4. 使ReportViewer可见
  5. 设置LocalReport.ReportPath = "Reports\\" = reportName;
  6. 清除LocalReport.DataSources.Clear()
  7. 添加新的LocalReport.DataSources.Add(new ReportDataSource(dataSource, dt));
  8. ReportViewer上的
  9. RefreshReport()
  10. 以下是设置并显示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();
    }
    

    如何删除所有旧的剩余数据?我是否处置了对象并重新创建它?

1 个答案:

答案 0 :(得分:2)

我明白了。我需要在方法的开头添加:reportViewer1.Reset();