How to link datasource dynamically with reportviewer in windows C # forms

I created a window form that acts as a report loader. I also created two RDLC reports from the Report Wizard, and he automatically created a dataset for these two reports. Now I have two data sets: sparcsn4DataSet.xsd and sparcsn4DataSet1.xsd, which use the stored procedure and pass two parameters (dateFrom / dateTo). I have a problem with binding to a data set depending on state:

if (idRep.Equals("extraMove")) 

Simply link the dataset to the report if you have one report.

Binding but what if you have a few? I can always create another form with another Reportviewer, but this is not an option (what if you have 10 reports / datasets), it definitely can not be considered?

There must be a way to bind the dataset to the reportviewer ... Does anyone have an idea how I can solve the status binding problem?

 if (idRep.Equals("extraMove")) { this.AGCT_ServiceEventReportTableAdapter.Fill(this.sparcsn4DataSet.AGCT_ServiceEventReport, d1,d2); } else if (idRep.Equals("stripStuff")) { this.AGCT_StripStuffReportTableAdapter.Fill(this.sparcsn4DataSet1.AGCT_StripStuffReport, d1, d2); } else { MessageBox.Show("Ooops, something went wrong...!"); } 

This is ReportForm.cs that has Reportviewer on it:

 namespace NavisReportLoader { public partial class ReportForm : Form { public DateTime d1; public DateTime d2; public string dat1; public string dat2; public string idRep; public ReportForm() { InitializeComponent(); } public void passParam(string dateFrom, string dateTo, string date1, string date2) { //ispravi ovo d1 = Convert.ToDateTime(dateFrom); d2 = Convert.ToDateTime(dateTo); dat1 = date1; dat2 = date2; } public void report(string id) { idRep = id; } private void ReportForm_Load(object sender, EventArgs e) { ReportParameter[] param = new ReportParameter[2]; param[0] = new ReportParameter("date1", dat1); param[1] = new ReportParameter("date2", dat2); this.reportViewer1.LocalReport.SetParameters(param); if (idRep.Equals("extraMove")) { this.AGCT_ServiceEventReportTableAdapter.Fill(this.sparcsn4DataSet.AGCT_ServiceEventReport, d1,d2); } else if (idRep.Equals("stripStuff")) { this.AGCT_StripStuffReportTableAdapter.Fill(this.sparcsn4DataSet1.AGCT_StripStuffReport, d1, d2); } else { MessageBox.Show("Ooops, something went wrong...!"); } this.reportViewer1.RefreshReport(); } } } 
0
source share
3 answers

Well, trying to figure it out, yesterday I found a solution that was acceptable to me, so I wanted to share with others:

1.st you need to create a class model with properties so that it is added to the dataset: Example:

 namespace NavisReportLoader.App_Data { public class ExtraMoveModel { public string EventType { get; set; } public int EventCount { get; set; } public int Num20 { get; set; } public int Num40 { get; set; } public int Num45 { get; set; } public int TEU { get; set; } public float Cargo { get; set; } public float Tare { get; set; } public float Total { get; set; } } } 

after that you need to create a simple simple class to connect to the database and call the stored procedure, pass the parameters and read it using the data output. In my example, I added this to the list and listed my model:

Example:

 public class ExtraMoveDataSet { string connectionString = @"Data Source=sampleDB; Initial Catalog=test; User Id=sa; Password=test"; public IEnumerable<ExtraMoveModel> extraMove(DateTime dateFrom, DateTime dateTo) { var tempList = new List<ExtraMoveModel>(); //string connectionString = @"Data Source=nsqltest; Initial Catalog=sparcsn4; User Id=sa; Password=lo02Nova"; SqlConnection conn = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("AGCT_ServiceEventReport", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@dateFrom", dateFrom); cmd.Parameters.AddWithValue("@dateTo", dateTo); conn.Open(); using (var dr = cmd.ExecuteReader()) { while (dr.Read()) { var temp = new ExtraMoveModel(); temp.EventType = dr["event_type"].ToString(); temp.EventCount = Convert.ToInt32(dr["CNT"]); temp.Num20 = Convert.ToInt32(dr["NUM20"]); temp.Num40 = Convert.ToInt32(dr["NUM40"]); temp.Num45 = Convert.ToInt32(dr["NUM45"]); temp.TEU = Convert.ToInt32(dr["TEU"]); temp.Cargo = float.Parse(dr["Cargo"].ToString(),new CultureInfo("hr-HR")); temp.Tare = float.Parse(dr["Tare"].ToString(),new CultureInfo("hr-HR")); temp.Total = float.Parse(dr["Total"].ToString(),new CultureInfo("hr-HR")); tempList.Add(temp); } } conn.Close(); return tempList; } 
  1. step - create a data set that will have the same name as the model properties.

enter image description here

  1. create a report on which you will bind a data set.

enter image description here

  1. Finally, you can add it to reportViewer1

     private void ReportForm_Load(object sender, EventArgs e) { ExtraMoveDataSet emDS = new ExtraMoveDataSet(); if (idRep.Equals("extraMove")) { ReportParameter[] param = new ReportParameter[2]; param[0] = new ReportParameter("date1", dat1); param[1] = new ReportParameter("date2", dat2); //string path = Directory.GetCurrentDirectory(); //string replace = path.Replace("\\bin\\Debug", "") + "\\App_Data\\"+"ReportExtraMove.rdlc"; var ret = emDS.extraMove(d1, d2); ReportDataSource rds = new ReportDataSource("extraMove", ret.ToArray()); this.reportViewer1.LocalReport.DataSources.Add(rds); //this.reportViewer1.LocalReport.ReportPath = replace; this.reportViewer1.LocalReport.ReportEmbeddedResource = "NavisReportLoader.App_Data.ReportExtraMove.rdlc"; this.reportViewer1.LocalReport.SetParameters(param); this.reportViewer1.RefreshReport(); } } 

I hope this helps others speed up the process.

Hooray!

+5
source

To force Visual Studio to automatically create a mediation source, you must first create a report (.rdlc). When connecting a data source, when you add a component (reportviewer) and bind the component to the name of the report, as shown in the image, after selecting the report, it will automatically create a binding source with the name that you use when you created the design.

0
source

I spent some time on the same issue. I am using VS 2013 where a dataset is connected via an automatically generated BindingSource in a report form. If you create a report and add all the necessary elements, everything works fine. If you (say) add a table later, BindingSources fail or are not created.

Edit the Design report form, click on the ReportViewer object and use the small Tasks arrow at the top of the rh corner. Either select “Rebind Data Sources” or “Choose Data Sources” to verify that they are correctly linked.

0
source

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


All Articles