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

How to set subReport reportsource dynamically

3 Answers 848 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Mary
Top achievements
Rank 1
Mary asked on 09 Jun 2016, 04:49 AM

I have a main report having subreport whose reportsource has to be set dynamically based on the field value (Response type) of the sqldatasource of mainreport ,
I tried to get the value of Responsetype from the dataobject in detail_itemdatabinding event and stored in a variable .
I subreport_itendataBinding event tried to set Telerik.Reporting.SubReport.reportsource to new object of TypeReportSource class, then invalidcastexception is ocurring
Unable to cast object of type 'Telerik.Reporting.Processing.SubReport' to type 'Telerik.Reporting.SubReport
'Telerik.Reporting.Processing.SubReport does not have REportsource property so i used Telerik.Reporting.SubReport, but exception is happening

Kindly assist on this issue

 public partial class InterviewReport : Telerik.Reporting.Report
    {
        protected string reportSourceValue = "";
        public InterviewReport()
        {
            //
            // Required for telerik Reporting designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        private void detail_ItemDataBinding(object sender, System.EventArgs e)
        {
            Telerik.Reporting.Processing.DetailSection item =
                (Telerik.Reporting.Processing.DetailSection)sender;
         
            object o = item.DataObject["ResponseType"];
            if (o is string)
            {
                if (string.Equals(o, "Text Response"))
                {
                    this.reportSourceValue = "ReportLibrary.Reports.Response, ReportLibrary, Version=1.0.0.0, Culture=neutral, " +
     "PublicKeyToken=null";
                }
                else if (string.Equals(o, "Check Box"))
                {
                    this.reportSourceValue = "ReportLibrary.Reports.ReportCheckBox, ReportLibrary, Version=1.0.0.0, Culture=neutral, " +
      "PublicKeyToken=null";
                }
            }
        }

        private void subReport1_ItemDataBound(object sender, System.EventArgs e)
        {
            // Telerik.Reporting.Processing.ReportItemBase item =
            // (Telerik.Reporting.Processing.ReportItemBase)sender;
            Telerik.Reporting.SubReport sub = (Telerik.Reporting.SubReport)sender;

            Telerik.Reporting.TypeReportSource typeReportSource2 = new Telerik.Reporting.TypeReportSource();
            typeReportSource2.Parameters.Add(new Telerik.Reporting.Parameter("LocalEngID", "= Parameters.LocalEngID.Value"));
            typeReportSource2.Parameters.Add(new Telerik.Reporting.Parameter("InterviewQuestionsID", "=Fields.InterviewQuestionsID"));
            typeReportSource2.TypeName = reportSourceValue;
            sub.ReportSource = typeReportSource2;
        }

    }

 

Regards,

Mary Jain

3 Answers, 1 is accepted

Sort by
0
Accepted
Stef
Telerik team
answered on 10 Jun 2016, 09:59 AM
Hello Mary,

In the SubReport item's event you will get Telerik.Reporting.Processing.SubReport element as sender. From the processing element you can access the ItemDefinition (Telerik.Reporting.SubReport).
For more details, please check Understanding Events.

The case can be handled without events and code. If you need to set the sub report depending on a field of the main report, you can add a SubReport Item and configure its ReportSource via binding and a custom function. The settings are illustrated in the attached demo project.

I hope this information is helpful.

Regards,
Stef
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
0
Mary
Top achievements
Rank 1
answered on 10 Jun 2016, 06:03 PM

Thanks, i used Binding with InstanceReportSource.The example given is more easy.

thanks one again

0
Accepted
Ian
Top achievements
Rank 1
answered on 06 Jun 2017, 04:08 AM
I wanted to add to this, that I updated to the new release of Reporting and my code broke and the SubReport was no longer rendering. Refactoring to use a Function in the Binding Expression allowed the SubReport to render again. So if you had code similar to the code below and your subreport stops showing on the report trying this refactor.

     InstanceReportSource source = new InstanceReportSource()
                    {
                        ReportDocument = new BookingConfirmationReport() //doc
                    };

                    source.Parameters.Add(new Parameter("id", resv.Id));

                    this.BookingConfirmationSubReport.ReportSource = source;
Tags
General Discussions
Asked by
Mary
Top achievements
Rank 1
Answers by
Stef
Telerik team
Mary
Top achievements
Rank 1
Ian
Top achievements
Rank 1
Share this question
or