Spartan
Spartan

Reputation: 146

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

Upvotes: 9

Views: 16020

Answers (4)

user12018068
user12018068

Reputation: 1

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

Upvotes: 0

BionicCyborg
BionicCyborg

Reputation: 9

/* Makes a Datatable which is the value, Clearing Local is important and the name of what ever you call your dataset in your report better match */

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

Upvotes: 0

tezzo
tezzo

Reputation: 11105

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

Upvotes: 9

Monah
Monah

Reputation: 6784

you can do the following

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

hope that this will help you

Upvotes: 2

Related Questions