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

Setting SubReport DataSource

2 Answers 386 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
Iron
Veteran
Iron
David asked on 13 Jun 2012, 05:14 PM
I just updated one of my smaller reporting projects, containing 32 reports, with the latest Q2 2012 code.  Several of the reports contain sub reports.  I am, as of Q2 2012, unable to directly set the data source property.

In prior versions of the code I would do something like:

List<BusinessObject> iData = new List<BusinessObject>;
List<BusinessObject2> iSubData = new List<BusinessObject2>;

Report r = new Report();
r.DataSource = iData;
SubReport sr = (SubReport)r.Items.Find("subreportname", true)[0];
if (sr != null) {
     sr.ReportSource.DataSource = iSubData;
}


Under Q2 it does not seem to be possible to directly set the datasource.  All the instructions say "InstanceReportSource" however I cannot see how to use this with a sub report.  Can someone tell me what I am missing?  The online docs seem to imply I must use a need datasource event.  That would require a complete redesign of my "generic" report logic.

2 Answers, 1 is accepted

Sort by
0
David
Top achievements
Rank 1
Iron
Veteran
Iron
answered on 13 Jun 2012, 05:56 PM
Well, after a bit of debugging, I have arrived at a solution.  I do not know if it is the "official" way to do things but it seems to work:

                    if (r != null

                    {

                        ReportItemBase[] ribItems = r.Items.Find(subReportName, true);

                        if ((ribItems != null) &&

                            (ribItems.Length == 1))

                        {

                            SubReport sr = (SubReport)ribItems[0];

                            if (sr != null)

                            {

                                InstanceReportSource irs = sr.ReportSource as InstanceReportSource;

                                if (irs != null)

                                {

                                    Report rsd = irs.ReportDocument as Report;

                                    if (rsd != null)

                                    {

                                        rsd.DataSource = dataList;

                                    }

                                }

                            }

                        }

                    }


Where "r" is the report object hosting the sub report.  The "magic" seems to be in the casting of the sub reports "ReportSource" object into a "InstanceReportSource" object.  Once we get that we seem to have direct access to the report document from which the sub report will/was created.  That object then has the DataSource object we wish to set.  

Any other suggestions?
0
SabatonRocks
Top achievements
Rank 1
answered on 19 Jun 2012, 08:15 PM
Your solution worked for me. Thanks!
Tags
General Discussions
Asked by
David
Top achievements
Rank 1
Iron
Veteran
Iron
Answers by
David
Top achievements
Rank 1
Iron
Veteran
Iron
SabatonRocks
Top achievements
Rank 1
Share this question
or