Merge blocks repeat content and included merge fields based on given data rows of a specific child table or object. Content within this block can be filtered and sorted with build-in functionality of the MailMerge TX Text Control .NET for Windows Forms
DocumentServer Namespace
MailMerge Class
The MailMerge class is a .NET component that can be used to effortlessly merge template documents with database content in .NET projects, such as ASP.NET web applications, web services or Windows services.
class. This demo shows how to vertically merge table cells in case the content is the same.

Sample Merge Block

Consider the following merge block template:

Merge Block

After merging some data into this block, the MailMerge class produces the following output:

Merge Block

In a post-merge process, the vertically adjacent cells with the same content should be merged as shown in the next illustration:

Merge Block

Mark Merge Block Tables

In a first step, a BlockRowMerged TX Text Control .NET for Windows Forms
DocumentServer Namespace
MailMerge Class
BlockRowMerged Event
Occurs when a merge block row has been merged successfully.
event is attached to mark the resulting tables of merge blocks. In a later process, we will search for these tables to process them:

Loading...

In that event, the first cell is marked with the Name TX Text Control .NET for Windows Forms
TXTextControl Namespace
TableCell Class
Name Property
Gets or sets the cell's name.
block_identifier.

Loading...

After the MergeObject TX Text Control .NET for Windows Forms
DocumentServer Namespace
MailMerge Class
Merge Method
Merges a single instance of an arbitrary type into the loaded document template.
method created the document, we look for all tables with the table marker and call the MergeSimilarCells method:

Loading...

Comparing the Cells

The actual work is done in the MergeSimilarCells method that loops through all columns separately to find similar vertically adjacent cells with the same content. If cells are found with the same content, they are vertically merged and the cell's text is replaced with the common content.

Loading...

This method must loop through the table twice (the function calls itself at the end) as complete rows are potentially merged which could result in more adjacent cells with the same content.

Merge Block

If the data is changed, so that cells in the columns Quantity and Price match, results look similar to the table shown in the next screenshot:

Merge Block

The vertical text alignment can be changed in the template, so that the merged table content is rendered in the middle of the table cell to generate a more readable layout:

Merge Block

Sample Project

This sample shows the flexibility of the MailMerge functionality. At any point, it is possible to interact during the merge process using the events or it is possible to manipulate the document after the merge process is finished.

Test this on your own by looking at the sample that is hosted in our GitHub account. This demo uses the Windows Forms version, but the code works with WPF and ASP.NET as well.