Add DataSource to subreport after deserialization

2 posts, 0 answers
  1. Robert
    Robert avatar
    1 posts
    Member since:
    Jul 2013

    Posted 15 Apr 2015 Link to this post

    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?

  2. Nasko
    Admin
    Nasko avatar
    1045 posts

    Posted 17 Apr 2015 Link to this post

    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.

     
Back to Top