In this report, we want to write out the full name and address of one particular customer and additionally, his or her job title and company name only in the case that this customer comes from Boston. This example demonstrates how it is possible to conditionally populate data in the template using Docentric toolkit. The example is very similar to Templating single values example. 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 Customer class

After that, you should be at the following point:
Conditinal content image

Insert Field elements bound to the Customer's name and address by configuring their Binding Source and Path properties (for detail explanation please look at Templating single values example):
Conditinal content image

Then insert If element on the template:
Conditinal content image
Note that on the picture the pane Data Sorces Explorer is turned off, that does not mean that data sources are discarded; simply, we are not showing it to save the space on the template.

If element is present only as unbound element, and therefore its Name property has to be defined. Its value (true or false) will be set later, during rendering of the element when concrite data for population of the template become known. Let us give it the name IsFromBoston:
Conditinal content image

In our case, if the data source of generating report is going to be customer with address in Boston, then the additional Field elements bound to job title and company name should be shown, otherwise not. Insert within If element two Field elements binding them regularly to JobTitle and Company properties:
Conditinal content image

Finished template (with Design mode turned on) looks like:
Conditinal content image


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. But, before we render the template, we have to subscribe to ElementInitialize event. This segment of code looks like following:

// Instancing report engine, by assigning the data source
DocumentGenerator dg = new DocumentGenerator(DataAccess.GetCustomerById(2));

// Subscribe to the event ElementInitialize
dg.ElementInitialize += new ElementInitializeHandler(dg_ElementInitializeEventHandler);

// Generating report by specifying the report template and the resulting report (as file paths)
DocumentGenerationResult result = dg.GenerateDocument("example9.docx", "example9_output.docx");

// Examining potentially errors
if (result.HasErrors)
{
    foreach (Error error in result.Errors) Console.Out.WriteLine(error.Message);
}


For each unbound LD Element on the template the handler dg_ElementInitializeEventHandler is executed in order to set its value. The event ElementInitialize is fired in the DocumentGenerator (ReportEngine) at the moment when LD Element's value has to be determined. Therefore, check if the current LD Element that is being rendering is of type IfElementInstance and with the name “IsFromBoston”:

void dg_ElementInitializeEventHandler(object sender, ElementInitializeEventArgs e)
{
    // Check if the current rendering element is the kind of If element named as “IsFromBoston”
    if (e.Element is InstanceModel.IfElementInstance &&
       ((InstanceModel.IfElementInstance)e.Element).Name == "IsFromBoston")
    {
          // Set the value of the element
          ((InstanceModel.IfElementInstance)e.Element).Value = GetCustomerById(2).Address.City == "Boston";
    }
}


More detailed explanation about managing code behind you can find here.


Generated report looks like:

Conditinal content image

What happened here? Value of the If element has been set to true, and DocumentGenerator (ReportEngine) continue to parse all content within If element. In opposite case, if the value is set to false, the whole content including all nested LD Elements would be neglected.

Let us demonstrate the other case too - choose a customer with address outside Boston:

// Instancing report engine, by assigning the data source
DocumentGenerator dg = new DocumentGenerator(DataAccess.GetCustomerById(1));


Generated report with the customer not living in Boston now does not contain the data of his or her job title and company name:

Conditinal content image