我有一个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();
}
Run Code Online (Sandbox Code Playgroud)
新版本仍然不起作用,当我运行该程序时,它仍然要求数据来源.
我已经尝试过不同的组合,但这些都不起作用.组合如:
var binding = new BindingSource();
binding.DataSource = this.NGStoreV2DataSet.mag2;
ReportDataSource rds = new ReportDataSource("NGStoreV2DataSet", binding);
Run Code Online (Sandbox Code Playgroud)
要么
ReportDataSource rds = new ReportDataSourc("NGStoreV2DataSet", this.mag2BindingSource);
Run Code Online (Sandbox Code Playgroud)
编辑:我终于设法解决了这个问题!我使用错误的DataSet(NGStoreV2DataSet而不是DataSet1的报告数据集)感谢tezzo和Hadi的帮助;)
你需要设置两个ReportPath和DataSources:
YourReportViewer.LocalReport.ReportEmbeddedResource = "ReportViewerForm.Report1.rdlc"
YourReportViewer.LocalReport.DataSources.Clear()
YourReportViewer.LocalReport.DataSources.Add(New ReportDataSource("YourTableName", yourDataTable))
Run Code Online (Sandbox Code Playgroud)