Well, this time I will show how easy is to make reports using ECO.
Create a new ECO Winform Project, for this, go to File->New->Other select the Delphi Net Projects folder, do a double click in ECO Winforms Application.
Now, go to the tab Model View and expand the first item of the list clicking in the signal ‘+’, expand the Package1 item and perform a double click in the first item of the list.
Now create a model like below:
don’t forget to change the name of the two type ends in the association or the ECO will complain.
Now click in the first tab at the left of the Model View and a double click in the Winform.pas item.
configure the property ECOSpaceType of the component rhRoots to use its Model.
Delete the components EcoAutoFormExtender, ECOListActionExtender and ECOGlobalActions.
Add and component ReportGenerator component and set its OwnerForm property to the current form and change its name for rptECOReport.
* Add in the DataSources property, clicking in the button Add, the name PessoaList and the Value pointing to ehPerson. (this item in design time wouldn’t have to be important, however if you do not fill it you will find problems when trying to have access to the DataSource property when trying to access it by code, therefore it is recommended to fill it).
Add two components ExpressionHandle and a CurrencyManager.
Change the name of one of the ExpressionHandle to ehPerson and the other to ehContact and change the name of the CurrencyManager to cmhPerson.
ehPerson must have the following expression Person.allInstances->select(e expression|e.PersonContact->isEmpty), then set its property *AddDefaultNestings to true and change the property RootHandle to use rhRoot, the ehContact must have its expression self.Contacts and its RootHandle will have to point to cmhPerson.
cmhPerson must have its RootHandle property pointing to ehPerson and its BindingContext property pointing to the TWinForm.
* We change to true the AddDefaulNesting property so that our report understands that our object Contact is a Person and it has the same fields that a Person and allows we to use it, set to false the report would only show the object without attributes, not allowing that the same to be manipulated.
Now, add to two datagrids and three buttons.
Change the DataSource option of one DataGrid to use ehPerson and the other to use the ehContact.
Changes the Text property of one of the buttons to Create Person, with the following code in the Click event:
Changes the Text property of the other button to Create Contact, with the following code in the Click event:
if Assigned(cmhPerson.Element) then
vContact: = Person.Create(EcoSpace);
MessageBox.Show(‘ No person to add contacts ');
Add the following namespaces in the section use of your code: NineRays.Reporting.DOM, System.Resources,
Change the Text property of the other button to Generate Report, with the following code in the Click event:
ehPessoa.EnsureBindingList; //Update the list off Persons so our report keeps updated
ehContato.EnsureBindingList;// Update the list off Contacts so our report keeps updated
rptECOReport.DataSources.Clear; //clean the report DataSources list.
rptECOReport.DataSources.Add('PessoaList', ehPessoa.GetList);//Add our list updated to the DataSource
rptECOReport.DesignTemplate; //This method is used only to draw the report. After Drawn it won’t be used.
rptECOReport.LoadTemplate(Application.ExecutablePath.Trim('EcoReport.exe'.ToCharArray)+'\EcoReport.rst '); //Load the saved report designed by the DesignTemplate method. Attention the name of the report must be the same of the one saved in the DesignTemplate.
vPreviewForm: = PreviewForm.Create(rptECOReport);// we create the form to show the Preview of the Report.
rptECOReport.Prepare; //Prepare the report.
vPreviewForm.WindowState: = FormWindowState.Maximized; //Set the state of the window to Maximized.
vPreviewForm.ShowDialog; //Show the Preview Form.
on ee: Exception of the MessageBox.Show(ee.Message, 'ECO Report ', MessageBoxButtons.OK, MessageBoxIcon.Error); //If occur some error we show it to the user.
vPreviewForm.Dispose; //Free the memory resources.
Now execute the program and click in the button Generate Report and the screen of design will be shown, as below:
go to File menu - > New - > Blank Report and click in Ok.
With our new report opened add a PageHeader in our report, to do this click in the icon then click in the report and put the PageHeader at the top of the page. Go to the DataSources tab and go to the Item Special Fields, you will find fields to identify the page number the description of the document, total page and others, Click and drag the Page Number inside of the PageHeader and put it on the right side. (Attention: Always place the objects inside the one you want to show or the same will not be shown). You can place the title of your Report in the PageHeader if you want to print it in all report pages or you can place a Detail and write inside it, to appear only in the initial page. In our case we go to add a Detail, to do that click in the icon and put it below the pageHeader, after that put a Text inside the Detail using the first item of the left lateral bar, called TextBox, and change its Text property to ECO Report with 9Rays.
Lets start to manipulate the ECO objects.
Add a DataBand clicking in the icon and put it below of the Detail, click in the DataBand and change its DataSource property to PersonList (the Name given in the DataSource of rptEcoReport), attention: to manipulate the data to be shown all the components as Header, Detail and others will have to be placed inside of the main DataBand or the data won’t be shown.
Good, with our DataBand ready lets place a Header inside it, to do that click in the icon and draw it inside of the DataBand. After that, put a TextBox with the Text Name and other with the Text Address inside the Header.
After that, add an Detail inside the DataBand, go to the Data Sources and expand the PersonList item (Exactly name of rptEcoReport) click and drag the item Name inside of the Detail, make the same thing with the Address.
Well, now we go to add another DataBand inside of our Databand to show the Contacts of each Person, add the DataBand and setup its DataSource to point to PeersonList.Contacts.
After that, add a Header and a Detail inside of this DataBand as made in the first one, the only difference is that you will have to catch the fields that is inside the DataSource called PersonList - > Contacts and drag them as done previously.
You can add a pageFooter to if you want.
The final result must be as below:
Save the report with the name EcoReport in the same folder of the executable, now close the program and remove or comment the line:
Save the Project and run, add some Persons and some Contacts and take a look at the final result ;).
To download 9Rays enter in the site of the company: 9Rays
I hope you liked, see you next week!
Any doubts or comments post it in the blog, after all the blog is setuped to accept comments without having a blog account.