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 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;
    }
}
[Visual Basic]
Public Class Invoice
    Public Property Products() As List(Of Product)
        Get
            Return m_Products
        End Get
        Set
            m_Products = Value
        End Set
    End Property
    Private m_Products As List(Of Product)

    Public Property Customer() As Customer
        Get
            Return m_Customer
        End Get
        Set
            m_Customer = Value
        End Set
    End Property
    Private m_Customer As Customer
End Class

Public Class Product
    Public Sub New(Name As String, Price As [Decimal])
        Me.Name = Name
        Me.Price = Price
    End Sub

    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set
            m_Name = Value
        End Set
    End Property
    Private m_Name As String

    Public Property Price() As [Decimal]
        Get
            Return m_Price
        End Get
        Set
            m_Price = Value
    End Set
    End Property
    Private m_Price As [Decimal]
End Class

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);
[Visual Basic]
Dim invoice As New Invoice()

invoice.Products = New List(Of Product)()
invoice.Products.Add(New Product("Apple", 3.55D))
invoice.Products.Add(New Product("Banana", 2.4D))
invoice.Products.Add(New Product("Pineapple", 2.99D))

Dim invoices = New List(Of Invoice)()
invoices.Add(invoice)

mailMerge1.MergeObjects(invoices)

<< PreviousNext >>