以编程方式将报告分配给我的reportViewer

时间:2015-10-28 16:44:26

标签: c# reportviewer

我有一个reportViewer和多个报告(ex 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);
编辑:我终于设法解决了这个问题!我使用错误的DataSet(NGStoreV2DataSet而不是DataSet1的报表数据集) 感谢tezzo和Hadi的大力帮助;)

4 个答案:

答案 0 :(得分:7)

您需要同时设置ReportPathDataSources

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

答案 1 :(得分:2)

您可以执行以下操作

alloc]init]

希望这会对你有所帮助

答案 2 :(得分:0)

/ * 使数据表成为值,清除本地是重要的,并且您在报表中称为数据集的名称更匹配 * /

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

答案 3 :(得分:0)

 Me.ReportViewer1.LocalReport.ReportEmbeddedResource = "ProjectName.ReportName.rdlc"