Why won't binding to a child object property with rdlc Report work in vs2010?
A开发者_如何学JAVA while ago someone asked how to bind to a child object's property in a rdlc report. Question here.
The solution was to use an expression like this:
=Fields!ChildObject.Value.SomeProperty
I have recently tried to upgrade to version 10 of the reporting libraries (Microsoft.ReportViewer.WebForms and Microsoft.ReportViewer.Common) and for some reason this does not work anymore. I have got the report rendering and displaying all data fine except any which uses this technique. Instead of the property value i get the text: "#Error"
Why doesn't this work anymore? Anybody know how to to this in the new version?
I can confirm that this bug has been fixed in VS2010 SP1 ... but you have to mark all of the relevant classes as Serializable.
You can find a sample project on this site which shows a working version: http://wraithnath.blogspot.com/2011/04/reportviewer-object-datasource-nested.html
The author also mentions that your classes will need a parameterless constructor but I have gotten it to work with classes without the default constructor. Still, if you have marked everything as serializable and are still seeing the "#Error" message, give it a try with parameterless constructors.
See question: child objects in rdlc (Studio 2010RC)
And related bug report at: https://connect.microsoft.com/VisualStudio/feedback/details/553592/accessing-nested-objects-in-data-source-of-local-report-does-not-function
Found another link regarding this: http://social.msdn.microsoft.com/Forums/en-US/vsreportcontrols/thread/16bdd594-7056-4796-8d83-39910dab1651
You really don't have to flatten your objects. Instead you can bind multiple datasets to report. Then you can assign multiple report data source to your report via code. Here is the working sample:
List<Loan> loans = new List<Loan>();
loans.Add(GetLoanByLoanNumber(loanNumber));
LocalReport report = new LocalReport();
report.ReportPath = HostingEnvironment.MapPath("~/bin/Report/Receipt.rdlc");
ReportDataSource loanDetailsDataSource = new ReportDataSource();
loanDetailsDataSource.Name = "LoanDataSet"; //This refers to the dataset name in the RDLC file
loanDetailsDataSource.Value = loans;
report.DataSources.Add(loanDetailsDataSource);
ReportDataSource loanItemsDataSource = new ReportDataSource();
loanItemsDataSource.Name = "LoanItemsDataSet"; //This refers to the dataset name in the RDLC file
loanItemsDataSource.Value = loans[0].loanItems;
report.DataSources.Add(loanItemsDataSource);
ReportDataSource principalPaymentDataSource = new ReportDataSource();
principalPaymentDataSource.Name = "PrincipalPaymentDataSet"; //This refers to the dataset name in the RDLC file
principalPaymentDataSource.Value = loans[0].principalPayments;
report.DataSources.Add(principalPaymentDataSource);
ReportDataSource interestPaymentDataSource = new ReportDataSource();
interestPaymentDataSource.Name = "InterestPaymentDataSet"; //This refers to the dataset name in the RDLC file
interestPaymentDataSource.Value = loans[0].interestPayments;
report.DataSources.Add(interestPaymentDataSource);
Hope this will help someone !
精彩评论