In addition to this if we want to use a subreport in the End User Designer, then a lot of script code is required achieve the desired results. The complexity increases when working in the End User Designer as there is no option to create multiple reports simultaneously and the report layout for the subreport has to be saved on the disk to be used later. A typical code example can be found in the article – Script for Subreports – from our documentation.
In this article, I would like to present an enhanced version of the End User Designer (using the shipped sample) which allows creating and using subreports without writing a single line of code. This will enable us to save the report layouts in memory, which will be added to a list present in the toolbar. The image below will make it clearer:
Finally when we are done with the creation of report layouts for the subreport and drag a subreport control to the main report, we would have an option to select the subreport either from the list of reports which we added or specify it from a physical location. The next image will give some more idea about it:
Though it sounds pretty straightforward but there are lots of things which need to be considered when approaching it. Let us find out what it takes to achieve this functionality:
- The first step involves adding a combo box and a button to the End User Designer tool strip which we will use to save the report layouts in memory.
- Next steps involve adding two forms to the project which will act as a save dialog and the report selection dialog (once the reports are saved).
- Till now it sounds a pretty simple implementation but now starts the tricky part. First of all we need to check whether a control which is dropped in the main report is a Subreport and which section it has been dropped onto. The LayoutChanging event allows this check to be performed.
- If the condition in the previous step is true then the “Select Subreport” dialog box is popped up so that a report can be supplied to the control.
- We select a report and set it as a subreport, but there are many things which happen between selection and showing of the subreport. When a subreport is selected, a SubReportDataObject is created with three properties (subreport name, section name, and subreport data) where subreport data is the string form of the rpx file. Let me first make it clear that the RPX (report layout) file is an XML and we are using stream to save and load the report. The main issue which arises when saving the reports is that the XML tags are automatically modified and therefore we need to find some way to get it back to same pattern which an RPX file should have. This is mandatory else we would not be able to load the report in the subreport control.
- The reversion of the pattern to its original form has to be done via script and in the ReportStart event. We would not want to do this everytime we save and load a report, so the question is how to automate this process? To do this, we need to add a resource file to the project which contains two strings.:
- If ReportStart event is already present in the report script. This is required as it might be possible that a user has already written something to the script section of the main report.
- If ReportStart event is not present in the report script.
- Both these variables contain script code which is added to the main report automatically on the basis of the presence of the ReportStart event. The added script decodes the contents of the userdata and assigns the subreports accordingly.
- Now we should be able to display the subreport properly on preview; however there are still many conditions which have to be checked once a report is assigned to the subreport control. It is possible that a user moves the subreport to a different location/ section, renames the control, renames the section containing the subreport. All these changes have to be considered as well.
Once all the steps have been completed, we should be ready to use the design reports using subreports in the End User Designer. If it sounds too complex then you should take a look at the – Enhanced – End User Designer – sample application where I have demonstrated how to incorporate all the above mentioned steps. To make it clearer, I have divided the additional code (apart from the code which is already present in the shipped End User Designer sample) into different sections. I hope this would help users to design their reports quickly and effectively. The Video below will demonstrate its functionality.