Example: Templating tables
Docentric Toolkit Help
In this report, we want to write a list of orders with some order details (e.g. shipping address, shipping fee, date of the order etc.). We want to arrange these data in Word table form. Data model used for all examples is exposed here.
Initially, we are creating a report template. Follow the common preparation steps to start building the template from scratch:
- Open a new blank Word document and enable it for templating
- Turn on Data Sources and Elements Explorers
- Define default data source as the Order class
After that, you should be at the following point:
We want to get on the report a list of orders instead of one single order. Thus, the data source should be treated as a collection of items which are typed as Order class. To express this, mark that the default data source of the type Order is going to be used as a collection:
Now, to display particular data of a single order, we have to add Docentric Field elements
within just inserted List element bound to the collection of orders.
Position yourself in cells of the templated row by Docentric List element and add several Field elements;
each of them should be bound to some of the Order property members
(about managing Field elements you can read here).
Let's examine how to set binding of a Docentric Field element to the property order date. Click on the Binding Control of the Value property of the element:
On the pop-up of the Binding Control of the Value property, select the Data Context which is the current data context inherited from the parent Docentric element (the List element) as a (Binding) Source property. Since the List element is bound to the collection of orders, current Data Context refers to an item of the type Order. So, in the other part of the Binding Control's pop-up all property members of the Order class are listed. Choose the OrderDate as a (Binding) Path property:
The result of the whole procedure of the binding setup for the Field element looks like following:
In the end, we have also defined a Format String of this Field element as a short date (about formatting values you can read here).
Finally, the templated table (with Design Mode switched on) looks like:
Next, save the template and generate the report. Report generation takes place programmatically, from your application that is using (has a reference to) the Docentric Report Engine dll. This code looks like following:
// Instancing report engine, by assigning the data source
List<Order> ordersDataSource = DataAccess.GetOrders();
DocumentGenerator dg = new DocumentGenerator(ordersDataSource);
// Generating report by specifying the report template and the resulting report (as file paths)
DocumentGenerationResult result = dg.GenerateDocument("example4.docx", "example4_output.docx");
// Examining potentially errors
foreach (Error error in result.Errors) Console.Out.WriteLine(error.Message);