Programmatically Assign a Report to My ReportViewer

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

I managed to assign different reports, but when I run the program, it says that I need to assign data sources, how can I do this?

EDIT: Here is my code:

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 does not work, when I run the program, it still asks for the origin of the data.

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

 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 it! I used the wrong DataSet (NGStoreV2DataSet instead of the report dataset, which is DataSet1) Thanks both tezzo and Hadi for a lot of help;)

+5
source share
4 answers

You need to install both ReportPath and DataSources :

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

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 this helps you

+2
source

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

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

Source: https://habr.com/ru/post/1234715/


All Articles