Sunday, March 11, 2012

another question about bind object to Report

hi,all
I download the Sample from http://www.gotreportviewer.com
in the "InvoiceMaker" sample,
the object like Custome can be bind to the Report directly,
but in the "Object Data Source" Sample ,I Modify the
" reportViewer.LocalReport.DataSources.Add(
new ReportDataSource("Product", merchant.GetProducts()));"
to "reportViewer.LocalReport.DataSources.Add(
new ReportDataSource("Product", new new Product("Pencil", 30)))"
there was an Exception with infomation:"Value does not fall within the expected range "


what's wrong with it?

LocalReport.DataSources takes a collection of objects, it doesn't allow a single element - that's why you get the error.

The reason why the InvoiceMaker works with a single element is because it uses a System.Windows.Forms.BindingSource - which can take either a list, an object or a type.

Here's the "magic" that makes it work in InvoiceMaker

Me.CustomerBindingSource = New System.Windows.Forms.BindingSource(Me.components)

...

Me.CustomerBindingSource.DataSource = GetType(ReportFromGrid.Customer)

...

ReportDataSource1.Value = Me.CustomerBindingSource

...

Me.CustomerBindingSource.DataSource = New Customer()

The ObjectDataSource sample takes a shortcut, and shows you how to use a collection directly, w/o going through the binding. That makes things simpler but removes some of the syntactic sugar that makes it easier to use.

Hope that helps,

Tudor Trufinescu

|||I know,thanks!

No comments:

Post a Comment