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

Add DataSource to subreport after deserialization

1 Answer 46 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 15 Apr 2015, 06:56 AM

Hi.

My report class looks like this:

public partial class SystemReport<THeader, TBody> : Report
        where THeader : Report, new()
        where TBody : Report, new()
    {
        public THeader ReportHeader { get; private set; }
        public TBody ReportBody { get; private set; }
 
        public SystemReport()
        {
            InitializeComponent();
            InitializeSubReportHeader();
            InitializeSubReportBody();
        }
 
        private void InitializeSubReportHeader()
        {
            ReportHeader = new THeader();
            headerReportSource = new InstanceReportSource { ReportDocument = ReportHeader };
            InitSubReport(ReportHeader, subReportHeader, headerReportSource);
        }
 
        private void InitializeSubReportBody()
        {
            ReportBody = new TBody();
            bodyReportSource = new InstanceReportSource { ReportDocument = ReportBody };
            InitSubReport(ReportBody, subReportBody, bodyReportSource);
        }
 
        private void InitSubReport(Report report, SubReport subReport, ReportSource reportSource)
        {
            ((ISupportInitialize)(report)).BeginInit();
            subReport.ReportSource = reportSource;
        }
    }

 

The way I create the report where OperationHeader and OperationBody are seperate subReports:

var report = new SystemReport<OperationHeader, OperationBody> {DataSource = model};
report.ReportBody.DataSource = model;
report.ReportHeader.DataSource = model;

After assigning above created report to reportViewer.ReportSource everything is displayed correctly.  

Now I want to serialize this report. It seems to be possible using ReportXmlSerializer class. The think is that when the report is deserialized, type of report is no longer SystemReport but Report base type.

As I understood it correctly when report is deserialized I have to assign once again data sources. I'm doing it in this way:

foreach (var item in systemReport.Items.OfType<Telerik.Reporting.DetailSection>())
            {
                var subReport = item.Items.OfType<Telerik.Reporting.SubReport>().Single();
                subReport.Report.DataSource = model;
            }
 
            foreach (var item in systemReport.Items.OfType<Telerik.Reporting.ReportHeaderSection>())
            {
                var subReport = item.Items.OfType<Telerik.Reporting.SubReport>().Single();
                subReport.Report.DataSource = model;
            }
 
            systemReport.DataSource = model;

But actually after assigning report to reportViewer.ReportSource, subReports are not rendered at all.

Do you have any idea what I'm doing wrong? Or is there any other way to solve this?

1 Answer, 1 is accepted

Sort by
0
Nasko
Telerik team
answered on 17 Apr 2015, 02:56 PM
Hello Robert,

The SubReport.Report Property used in the provided code snippets "gets the report this item belongs to", as described in the Reporting API Reference.

This means the code:
subReport.Report.DataSource = model;
is setting the DataSource of the main report, which is not what is desired.

In order to set the data source of the sub-report you need to use the following code instead:
var subReport = item.Items.OfType<Telerik.Reporting.SubReport>().Single();
var instanceReportSource = subReport.ReportSource as Telerik.Reporting.InstanceReportSource;
var report = instanceReportSource.ReportDocument as Telerik.Reporting.Report;
report.DataSource = model;

Regards,
Nasko
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
General Discussions
Asked by
Robert
Top achievements
Rank 1
Answers by
Nasko
Telerik team
Share this question
or