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

Subreport binding to parent report objectdatasource. Seeing the sub report, but there's no data in it

1 Answer 180 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Czeshirecat
Top achievements
Rank 1
Czeshirecat asked on 08 Nov 2017, 11:47 AM

I'm almost there. Subreport is displaying but theres no data in it. What am I doing wrong please?

Winforms. ObjectDatasource using a dataset with 2 tables, master and detail.

My report represents a paper questionnaire/Form. Master is a single row table with a person's personal details eg name, address etc. The details table is multi row and represents the questions and answers that the person filled out. The tables are linked by an ID field. Ive added the ID to the filter field. For testing purposed I've created a dummy main and multi detail records.

 

Main report designer file entries

//
 // HealthQuestionnaireDataSet
 //
 this.HealthQuestionnaireDataSet.DataMember = "MainDetail";
 this.HealthQuestionnaireDataSet.DataSource = typeof(pulse.move.datasources.datasetHealthQuestionaire);
 this.HealthQuestionnaireDataSet.Name = "HealthQuestionnaireDataSet";
//
// subReport1
//
this.subReport1.Name = "subReport1";
typeReportSource1.Parameters.Add(new Telerik.Reporting.Parameter("parmId", "= Fields.Id"));
typeReportSource1.TypeName = "pulse.move.telerik.reportlib.RptHealthQuestionnaireSubDetail, pulse.move.telerik.reportlib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
this.subReport1.ReportSource = typeReportSource1;

 

I call the following static method in the code behind of my main report to generate the report onto the ReportViewer  using the dataset passed in. This seems to work correctly. If I change the code to read "var report = new RptHealthQuestionnaireMainDetail { DataSource = dataset };" then it fails to generate. I wondered if this was the cause of my problem with the sub report not working.

public static void GenerateReport(datasetHealthQuestionaire dataset, ReportViewer viewer)
{
  viewer.CancelRendering();
  var report = new RptHealthQuestionnaireMainDetail { DataSource = dataset.maindetail };
  var reportSource = new InstanceReportSource {ReportDocument = report};
  viewer.ReportSource = reportSource;
  viewer.RefreshReport();
}

I wasn't able to get the subreport to work in the designer, so I moved the binding code to the constructor of the sub report to make it easier to debug and change.

The subreport displays the labels correctly, but there's no data. What am I missing to bind the data correctly?

public RptHealthQuestionnaireSubDetail()
{
  //
  // Required for telerik Reporting designer support
  //
  InitializeComponent();
 
  Bindings.Clear();
  // tried both of the following. Makes no difference. Still no data
  //Bindings.Add(new Telerik.Reporting.Binding("DataSource", "=ReportItem.DataObject"));
  Bindings.Add(new Telerik.Reporting.Binding("DataSource", "=ReportItem.Parent.Parent.DataObject"));
       
}

 

//
// RptHealthQuestionnaireSubDetail
//
// Commented out the following 2 lines and moved them to the constructor
//this.Bindings.Add(new Telerik.Reporting.Binding("DataSource", "=ReportItem.DataObject"));
//this.DataSource = this.HealthQuestionnaireDataSource;
this.Filters.Add(new Telerik.Reporting.Filter("= Fields.Id", Telerik.Reporting.FilterOperator.Equal, "= Parameters.parmId.Value"));
this.Items.AddRange(new Telerik.Reporting.ReportItemBase[] {
      this.detailSection1});
this.Name = "RptHealthQuestionnaireSubDetail";
reportParameter1.Name = "parmId";
reportParameter1.Type = Telerik.Reporting.ReportParameterType.Integer;
this.ReportParameters.Add(reportParameter1);

1 Answer, 1 is accepted

Sort by
0
Todor
Telerik team
answered on 13 Nov 2017, 01:32 PM
Hi Czeshirecat,

The way you assign the DataSource of the main report you choose it to be the main table of the Dataset. Note that if you assign the Dataset as a DataSource, by default the first table of the Dataset would be used. For details I could refer you to the How to bind to a DataSet article.
The main table is set also as DataSource for the Subreport (through the binding "DataSource", "=ReportItem.DataObject"). However, since in the main table there is no data relevant to the Subreport, it would remain empty.

Note that Telerik Reporting supports binding directly to IEnumberable/IListSource objects (incl. arrays, collections, DataSet, DataTable, DataView, DbDataAdapter) for backwards compatibility. Instead consider using the ObjectDataSource that can handle this types of data sources.

To populate the subreport, one option would be to create a separate DataSource for the Subreport, and set it to the Dataset detail table.

Another option would be to use the current approach with both tables merged into a single one. This way it would be possible to bind the DataSource of the SubReport to the one of the main report, and use only the relevant fields. See the How to bind subreport to main reports data KB article for details.

Regards,
Todor
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Czeshirecat
Top achievements
Rank 1
Answers by
Todor
Telerik team
Share this question
or