Technical Article: Using Objects as Data Sources

The direct use of enumerable business objects as a data source to merge templates is one of the typical scenarios with TX Text Control Reporting.

Use IEnumerable Objects as Data Sources

MailMerge interprets all public properties of objects in the collection as table columns and child tables. Properties of type IEnumerable are automatically instantiated as relations and are used for merge blocks and nested merge blocks.

Consider the following class structure as a data source:

Below is the C# code that represents the above UML class design:

[C#]
public class Invoice
{ 
	public List<Product> Products 
	{
		get;
		set;
	}

	public Customer Customer
	{
		get;
		set;
	}
}

public class Product
{
	public Product(string Name, Decimal Price)
	{
		this.Name = Name;
		this.Price = Price;
	}

	public string Name
	{
		get;
		set;
	}

	public Decimal Price
	{
		get;
		set;
	}
}

The template consists of a merge block named Products. The merge fields in the block are named Name and Price.

The following code shows how to create the data source object and how to start the merge process using MergeObjects:

[C#]
Invoice invoice = new Invoice();

invoice.Products = new List<Product>();
invoice.Products.Add(new Product("Apple", 3.55m));
invoice.Products.Add(new Product("Banana", 2.4m));
invoice.Products.Add(new Product("Pineapple", 2.99m));

var invoices = new List<Invoice>();
invoices.Add(invoice);

mailMerge1.MergeObjects(invoices);