以编程方式将报表分配给我的报表查看器

本文关键字:报表 我的 分配 编程 方式 | 更新日期: 2023-09-27 18:19:42

我有一个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的报表数据集)感谢特佐和哈迪的大力帮助;)

以编程方式将报表分配给我的报表查看器

您需要同时设置ReportPathDataSources:

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

您可以执行以下

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();

希望这将帮助您

/*生成一个Datatable,它是值,Clearing Local很重要,并且您在报告中称之为数据集的名称更好地匹配*/

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();
 Me.ReportViewer1.LocalReport.ReportEmbeddedResource = "ProjectName.ReportName.rdlc"