Example: Sorting and grouping
Docentric Toolkit Help
In this report, we want to write a list of products with some product details (e.g. name, category, price etc.) grouping them by the product category and at the same time, sorting them due to price in ascending order within each of category. 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 Product class
Add LD Group element to the template in a way that
the whole Word table with label Category are comprised:
Define its property Binding Source to default that means
IEnumerable<Product> and leave the Path property empty.
Then define the key (Group By property) that grouping will be performed
by. In our case this is the name of the product category:
However, Group element reshapes collection of values that it is bound to to collection of groups formed due to chosen key. Each group (both key and values) in this collection represents providing Data Context for LD element's inserted within this Group element.
Thus, within our Group element we can insert a Field element bound to the group key (or some of its inherited property):
As it is shown above the Path property of this Field element is set to Key, and the Key was previously determinated as key of the grouping i.e. Group element, in our case as a property Category.Name of type of grouping collection's item Product:
Also, within Group element a List element naturally found its position in order to be bound to a singular group:
We embraced the whole row within List element. Binding Source property is set to Current Data Context whereas Path property of the List element is left empty. Next, we have to sort each of groups of products (grouped by group key which is name of product category) by product price. This is defined with Order By property:
Now, within List element insert right Field elements bound to product name and price. Field element bound to Product's property Price should be formatted as a currency using Format String property (about formatting values you can read here):
Also, this collection of groups ("result" of rendering of Group element) could be sorted as well by the grouping key or some of its inherited property.
It is also possible to group collections by more than one grouping key. To achieve this, nested Group elements should be used.
Next, save the template and generate the report. Report generation takes place programmatically, from your application that is using (has a reference to) the LD ReportEngine dll. This code looks like following:
// Instancing report engine, by assigning the data source
DocumentGenerator dg = new DocumentGenerator(DataAccess.GetProducts());
// Generating report by specifying the report template and the resulting report (as file paths)
DocumentGenerationResult result = dg.GenerateDocument("example6.docx", "example6_output.docx");
// Examining potentially errors
foreach (Error error in result.Errors) Console.Out.WriteLine(error.Message);
It is also possible to invoke the method GenerateDocument with same parameters typed as System.IO.Stream.