Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically assign report to my reportViewer

I have a reportViewer and multiple reports (ex Report1.rdlc, Report2.rdlc, ecc), how i can switch between them programmatically?

I was able to assign the different reports but when i execute the program it says that i need to assign data origins, how can i achieve to do that?

EDIT: Here is my code so far:

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

The new version still doesn't work, when i run the program it still ask for data origins.

I have already tried different combinations, but none of this works. combinations like:

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

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

or

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

EDIT: I finally managed to solve this!! i was using the wrong DataSet (NGStoreV2DataSet instead of the report dataset wich is DataSet1) Thank to both tezzo and Hadi for the great help ;)

like image 751
Spartan Avatar asked Oct 28 '15 16:10

Spartan


1 Answers

You need to set both ReportPath and DataSources:

YourReportViewer.LocalReport.ReportEmbeddedResource = "ReportViewerForm.Report1.rdlc"
YourReportViewer.LocalReport.DataSources.Clear()
YourReportViewer.LocalReport.DataSources.Add(New ReportDataSource("YourTableName", yourDataTable))
like image 157
tezzo Avatar answered Sep 17 '22 00:09

tezzo