Sometimes your work needs one and the same report, but with a few changes. For example, different headers for different departments. So, for example, a user of the ActiveReports report generator wants to create such a report:
For example, the user select copies for the customer and for administration department.
After, the application has to show the two pages in a form viewer.
Let's implement such a report in FastReport .NET. You can generate the same report and pass it different parameters for the header. But if you want to receive similar reports with different headlines in one report, you can do otherwise. We can duplicate the pages of the original report, and then modify the headers in these pages. The easiest way is to show on the report with one page.
The report will have a parameter that lists the number of titles that you want to display in this report through a comma. In the report script, we duplicate the page based on the number of values in the setting, then replace the text in the headlines of the new pages to the desired.
In this way, we can form pages with different titles depending on the numbers that are transferred. However, there is one difficulty. You'll have to manually calculate the number of the text object in which you want to replace the text. For example, there are 9 text objects in my report. The first is the headline. So the tenth will also be the headline, but on the new page. If you add another 9 to 10, we get the title number on the third page and so on.
It will become more clear when you the script code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
private void _StartReport(object sender, EventArgs e) { //Parse parameter string parameter = (string)Report.GetParameterValue("Parameter"); string[] arr = parameter.Split(','); //Number of text obj that is header int step = 10; foreach (string item in arr) { ReportPage newPage = new ReportPage(); newPage.AssignAll(Page1); Report.Pages.Add(newPage); newPage.CreateUniqueName(); foreach (object obj in newPage.AllObjects) { if (obj is Base) { (obj as Base).CreateUniqueName(); } } TextObject txt = Report.FindObject("Text"+ step.ToString()) as TextObject; switch (item) { case "1": txt.Text = "Header 1"; break; case "2": txt.Text = "Header 2"; break; case "3": txt.Text = "Header 3"; break; } step = step + 9; } Report.Pages.Remove(Page1); } |
Let's take a closer look at this script. We used the StartReport event at the Report object. In the event handler, we will manipulate the report pages. The report already has one page developed - in fact it is a ready-made "combat" report, on the basis of which we want to make several, but with different headlines.
First of all, we parse a report parameter. As mentioned earlier, the parameter is passed in sequence identifier patterns that we want to display. Identificators are separated by commas. Next, iterate loop parameter IDs obtained from the report. For each of them creates a page based on an existing text object is calculated with a header. Depending on the text object header template identifier is set. Once we have established a "pack" the necessary pages, remove the first page of the sample.
As a result, we get multiple reports within a single. A controlled number of these reports pages may be passing in the report parameter value - the desired combination of identifiers or headers.