This is a migrated thread and some comments may be shown as answers.

Bind report to an existing list of data

1 Answer 465 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
jeff
Top achievements
Rank 1
jeff asked on 15 Sep 2015, 05:05 PM

I would like to set the Datasource of my report to a business object (ObjectDataSource) that has all of it's data already populated.  ie. when the report requests the data, it does not need to go out to any datastore to retrieve it, the object is already initialized with the data it requires.

I have a class with a method that returns a list of objects.

    

public class SummaryReportData
{
   public List<SummaryData> RetrieveData( )
   {
      return SummaryDataList;
   }          
   public List<SummaryData> SummaryDataList { get; set; }  
}

 

When I am launching my report, I am retrieving the data I would like to display from my data service, and setting it on the object that the report will be requesting the data from.   Like so: 

var report = new MonthlySummaryReport();
             
var dataObject = new SummaryReportData();
dataObject.SummaryDataList = someDataService.GetSummaryReportData(100);
report.DataSource = dataObject;
 
ReportToDisplay = new InstanceReportSource();
ReportToDisplay.ReportDocument = report;

 (this is a wpf application, so I am databinding the 'ReportToDisplay' report source to the viewer.  This works as expected)

I then have the report setup to use an ObjectDataSource, Business Object set to "SummaryReportData", and the DataMember is set to "RetrieveData".

What ends up happening is, even though I have pre-initialized the SummaryReportData object with my data, when the report renders it is creating a new instance of that object, calling it's constructor, and then calling the "RetrieveData" method, which returns an empty list.

How do I get the report to use the object instance I am supplying it, instead of recreating it when it launches?

1 Answer, 1 is accepted

Sort by
0
Accepted
jeff
Top achievements
Rank 1
answered on 15 Sep 2015, 06:40 PM

Well, I solved this myself.

I did a more thorough search of the forum and found this post.

http://www.telerik.com/forums/objectdatasource-called-multiple-times

 

Which led me to examine my report more closely.  

I created the report using one of the report template/wizards provided by Telerik.  It's a fairly simple report containing a table with 3 columns of data.  What I did not realize is that the report itself does not have a DataSource set on it, but the table does.

Once I realized this, I altered my code to find the table object in the report, and set it's DataSource up properly, and removed the code that was setting the DataSource of the report directly.

This resolved the problem.

For reference, here is the correct working code:

var report = new MonthlySummaryReport();
var dataObject = new SummaryReportData();
dataObject.SummaryDataList = someDataService.GetSummaryReportData(100);
 
var dataSource = new ObjectDataSource();
dataSource.DataSource = dataObject;
dataSource.DataMember = "RetrieveData";
 
(report.Items.Find("summaryTable", true)[0] as Table).DataSource = dataSource;
 
ReportToDisplay = new InstanceReportSource();
ReportToDisplay.ReportDocument = report;
 
LoadDesignerData();

 

Thanks!

Tags
General Discussions
Asked by
jeff
Top achievements
Rank 1
Answers by
jeff
Top achievements
Rank 1
Share this question
or