问题
I have a following problem. I made an application that generates rdlc reports for one of my clients. I used TableAdapters to fill the DataTables that I use in those reports. The problem is that my client has a new database and he wants to be able to use that application to generate reports from both the old and the new one. I thought it was possible to change the connection that my TableAdapters use so that they would just take the data from the database that my client selects (both databases have identical schema's) but I was told that that cannot be done. So is it possible to change the DataTable that rdlc report uses? How can I solve this problem
回答1:
You can assign any datatable to rdlc at run time using following code.
DataTable dtTest =obj.SelectDepartment(1);//Here I am selecting the data from DB
this.reportViewer1.RefreshReport();
reportViewer1.Visible = true;
ReportDataSource rds = new ReportDataSource();
reportViewer1.Reset();
reportViewer1.ProcessingMode = ProcessingMode.Local;
LocalReport rep = reportViewer1.LocalReport;
rep.Refresh();
rep.ReportEmbeddedResource = "Report.rdlc";//Provide full path
rds.Name = "DataSet1_tblAdapter";//Provide refrerence to data set which is used to design the rdlc. (DatasetName_TableAdapterName)
rds.Value = dtTest;
rep.DataSources.Add(rds);
this.reportViewer1.RefreshReport();
回答2:
Sure, it should be fine to change your data source for the report dynamically
rptView.LocalReport.ReportPath = Server.MapPath("MyReportName")
rptView.LocalReport.DataSources.Add(New ReportDataSource("DataSource", "ObjectSource"))
Sorry bout the code being VB.net, I know your question is marked C#, but it should be similar enough
Edit:
If you wanted some Parameters
dim listOfParams = new List(of ReportParameter);
listOfParams.Add(new ReportParameter("Param1", myValue.toString()))
listOfParams.Add(new ReportParameter("Param2", myOtherValue.toString()))
Me.rptView.LocalReport.GetParameters(listOfParams)
Just coded that straight so it might well be a mix of C# and VB.net
来源:https://stackoverflow.com/questions/10124047/how-to-change-datatables-in-rdlc-reports-programmatically