在WinForms中的ReportViewer中切换数据源 [英] Switching DataSources in ReportViewer in WinForms

查看:160
本文介绍了在WinForms中的ReportViewer中切换数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为用户创建了一个winform,以查看我为他们创建的许多报告。我有一个带有报告名称的下拉列表,它会触发相应的字段来显示参数。填写完毕后,他们按Submit(提交),将显示报告。他们第一次点击屏幕时此方法有效。他们可以更改参数,ReportViewer可以正常工作。更改为其他报表,我得到以下ReportViewer错误:

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

就我使用的过程而言:


  1. 我设置了 reportName (字符串)物理RDLC名称。

  2. 我设置了 dataSource (字符串)作为数据源名称

  3. 我在通用DataTable中填充了要从中运行报表的数据。

  4. 使ReportViewer可见

  5. 设置 LocalReport.ReportPath = Reports\\ = reportName;

  6. 清除 LocalReport.DataSources.Clear()

  7. 添加新的 LocalReport.DataSources.Add(new ReportDataSource(dataSource,dt));

  8. RefreshReport()在ReportViewer上。

  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.

这是设置并显示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?

推荐答案

我发现了它。我需要在该方法的开头添加: reportViewer1.Reset();

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

这篇关于在WinForms中的ReportViewer中切换数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆