【问题标题】:Programmatically assign report to my reportViewer以编程方式将报告分配给我的 reportViewer
【发布时间】:2015-10-28 16:44:25
【问题描述】:

我有一个 reportViewer 和多个报告(例如 Report1.rdlc、Report2.rdlc、ecc), 我如何以编程方式在它们之间切换?

我能够分配不同的报告,但是当我执行程序时它说我需要分配数据来源,我该如何做到这一点?

编辑:到目前为止,这是我的代码:

public Report()
{
    InitializeComponent();

    this.View_StatoMagTableAdapter.Fill(this.NGStoreV2DataSet.View_StatoMag);
    this.mag2TableAdapter.Fill(this.NGStoreV2DataSet.mag2);

    this.mag2BindingSource.DataMember = "mag2";
    this.mag2BindingSource.DataSource = this.NGStoreV2DataSet;
}

private void reportViewer1_Load(object sender, EventArgs e)
{
    this.reportViewer1.Reset();

    var binding = new BindingSource();
    binding.DataSource = this.NGStoreV2DataSet.mag2;

    ReportDataSource rds = new ReportDataSource("NGStoreV2DataSet", binding);
    this.reportViewer1.LocalReport.DataSources.Clear();
    this.reportViewer1.LocalReport.DataSources.Add(rds);
    this.reportViewer1.LocalReport.ReportEmbeddedResource = "ReportViewerForm.Report2.rdlc";
    this.reportViewer1.RefreshReport();
}

新版本还是不行,运行程序时还是会询问数据来源。

我已经尝试了不同的组合,但这些都不起作用。 像这样的组合:

var binding = new BindingSource();
binding.DataSource = this.NGStoreV2DataSet.mag2;

ReportDataSource rds = new ReportDataSource("NGStoreV2DataSet", binding);

ReportDataSource rds = new ReportDataSourc("NGStoreV2DataSet", this.mag2BindingSource);

编辑:我终于设法解决了这个问题!我使用了错误的数据集(NGStoreV2DataSet 而不是报表数据集,它是 DataSet1) 感谢 tezzo 和 Hadi 的大力帮助;)

【问题讨论】:

  • 你是如何设置它的?你的实现在哪里?

标签: c# reportviewer


【解决方案1】:

你需要同时设置ReportPathDataSources

YourReportViewer.LocalReport.ReportEmbeddedResource = "ReportViewerForm.Report1.rdlc"
YourReportViewer.LocalReport.DataSources.Clear()
YourReportViewer.LocalReport.DataSources.Add(New ReportDataSource("YourTableName", yourDataTable))

【讨论】:

  • 谢谢,但我想念变量“yourDataTable”的来源。
  • yourDataTable 是一个数据表,其中填充了要在报告中显示的数据;在 MSDN 上查看此链接:msdn.microsoft.com/en-us/library/ms171920.aspx
  • 你的意思是这样的? this.mag2TableAdapter.Fill(this.NGStoreV2DataSet.mag2); ReportDataSource rds = new ReportDataSource("NGStoreV2DataSet", this.NGStoreV2DataSet.mag2);因为像这样它给了我错误
  • 确切的错误是什么?也许您在 rdlc 文件中定义了一个报告数据集,其名称不同于“NGStoreV2DataSet”。
【解决方案2】:

您可以执行以下操作

var binding = new BindingSource();
binding.DataSource = yourData;
reportViewer1.Reset();
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("NGStoreV2DataSet",
                    binding));
reportViewer1.LocalReport.ReportEmbeddedResource = "ReportViewerForm.Report1.rdlc";
reportViewer1.RefreshReport();

希望对你有帮助

【讨论】:

  • 它不起作用,它仍然要求数据来源,我用实际代码编辑了我的问题
【解决方案3】:

/* 制作一个作为值的数据表,清除本地很重要,并且您在报告中调用数据集的名称更好地匹配 */

DataTable dGraph = clsDailyReports.MakeTmpDataSet.Invoke(con, SqlAtd).Tables[0];

    rpt.LocalReport.DataSources.Clear();

    Microsoft.Reporting.WebForms.ReportDataSource rptdBody = new Microsoft.Reporting.WebForms.ReportDataSource();
    rptdBody.Name = "DataSet1";
    rptdBody.Value = dBody;
    rpt.LocalReport.DataSources.Add(rptdBody);

    Microsoft.Reporting.WebForms.ReportDataSource rptdTop = new Microsoft.Reporting.WebForms.ReportDataSource();
    rptdTop.Name = "DataSet2";
    rptdTop.Value = dGraph;
    rpt.LocalReport.DataSources.Add(rptdTop);

    DataTable dDate = clsDailyReports.MakeTmpDataSet.Invoke(con, sSqlDate).Tables[0];
    Microsoft.Reporting.WebForms.ReportDataSource rptDate = new Microsoft.Reporting.WebForms.ReportDataSource();
    rptDate.Name = "DataSet3";
    rptDate.Value = dDate;
    rpt.LocalReport.DataSources.Add(rptDate);

    rpt.LocalReport.ReportPath = System.Web.HttpContext.Current.Server.MapPath(@"~\Reports\rptUnAdjustedPeriodTotals.rdlc");
    rpt.LocalReport.Refresh();

【讨论】:

    【解决方案4】:
     Me.ReportViewer1.LocalReport.ReportEmbeddedResource = "ProjectName.ReportName.rdlc"
    

    【讨论】:

    • 你能解释一下这个答案与其他人推荐给某些 ReportViewer 的 set.LocalReport.ReportEmbeddedResource 有什么不同吗?
    猜你喜欢
    • 2013-02-03
    • 1970-01-01
    • 2023-04-07
    • 2018-06-23
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多