MailMerge: Conditional Rendering of Merge Blocks
Merge blocks can be used to render content conditionally. This article explains different ways to control this.

Typically, a merge block is used to repeat content such as table rows. But a merge block can be also used to control the rendering of a complete block such as a sub report.
Remove Merge Blocks by Removing Data
Consider the following data model:
public class Report
{
public string Name { get; set; }
public List<Product> Products { get; set; }
}
public class Product
{
public string Name { get; set; }
public float Price { get; set; }
}
The following screenshot shows the simple template for this sample. Highlighted in red, the merge block is shown:
The following code creates two instances of the data model. The first Report has a Products list and the second doesn't. If the MailMerge property TXText
List<Report> reports = new List<Report>();
Report report1 = new Report()
{
Name = "Test Report",
Products = new List<Product>() {
new Product() { Name = "TX Text Control Product 1", Price = 634.34F },
new Product() { Name = "TX Text Control Product 2", Price = 34.34F },
}
};
Report report2 = new Report()
{
Name = "Test Report2",
};
reports.Add(report1);
reports.Add(report2);
In the results, you can see that the Products section gets rendered in the first report, but is completely removed in the second report:
If the data is missing, the merge block gets removed.
Render Merge Blocks Based on Conditions
Another way to control the merge block rendering is to compare a value of the merge block's parent table. Therefore, we change the data model a little bit:
public class Report
{
public string Name { get; set; }
public bool RenderProducts { get; set; }
public List<Product> Products { get; set; }
}
public class Product
{
public string Name { get; set; }
public float Price { get; set; }
}
A boolean value has been added to the parent object that should control whether the merge block is rendered or not.
The TXText
The following codes creates the new data object. The Products data part is now available in both objects, but RenderProducts is used to specify whether the block is rendered or not:
reports = new List<Report>();
Report report1 = new Report()
{
Name = "Test Report",
RenderProducts = true,
Products = new List<Product>() {
new Product() { Name = "TX Text Control Product 1", Price = 634.34F },
new Product() { Name = "TX Text Control Product 2", Price = 34.34F },
}
};
Report report2 = new Report()
{
Name = "Test Report2",
RenderProducts = false,
Products = new List<Product>() {
new Product() { Name = "TX Text Control Product 1", Price = 634.34F },
new Product() { Name = "TX Text Control Product 2", Price = 34.34F },
}
};
reports.Add(report1);
reports.Add(report2);
This code is used to insert the merge block with the condition (if "RenderProducts" = true):
MergeBlockInfo mergeBlockInfo = new MergeBlockInfo("Products");
mergeBlockInfo.BlockMergingCondition =
new List<TXTextControl.DocumentServer.DataShaping.FilterInstruction>()
{
new TXTextControl.DocumentServer.DataShaping.FilterInstruction(
"RenderProducts",
TXTextControl.DocumentServer.DataShaping.RelationalOperator.Equals,
true
)
};
mergeBlockInfo.ColumnNames =
ribbonReportingTab1.DataSourceManager.PossibleMergeFieldColumns.Select(
column => column.ColumnName).ToList();
MergeBlockSettings settings = new MergeBlockSettings(BlockTemplateType.PlainParagraph);
ribbonReportingTab1.DataSourceManager.InsertMergeBlock(textControl1, mergeBlockInfo, settings);
The results are the same. The merge block is rendered in the first case and it is not rendered in the second case:
Also See
This post references the following in the documentation:
- TXText
Control. Document Server. Data Sources. Merge Block Info. Block Merging Condition Property - TXText
Control. Document Server. Mail Merge. Remove Empty Blocks Property
Reporting
The Text Control Reporting Framework combines powerful reporting features with an easy-to-use, MS Word compatible word processor. Users can create documents and templates using ordinary Microsoft Word skills. The Reporting Framework is included in all .NET based TX Text Control products including ASP.NET, Windows Forms and WPF.
Related Posts
ReportingWindows FormsMail Merge
DataSourceManager: Using the Ready-to-Use Reporting Dialog Boxes
The Text Control DocumentServer class comes with ready-to-use dialog boxes for various purposes. This article shows how to use them in your application.
TX Text Control 32.0 Has Been Released
We are pleased to announce the immediate availability of TX Text Control 32.0 for all platforms including ASP.NET, Windows Forms, WPF and ActiveX.
TX Text Control 31.0 and TX Spell .NET 10.0 Have Been Released
We are happy to announce the immediate availability of TX Text Control 31.0 for all platforms including ASP.NET, Windows Forms, WPF and ActiveX and TX Spell .NET 10.0 for all .NET based platforms.
TX Text Control 30.0 and TX Spell .NET 9.0 Have Been Released
We are happy to announce the immediate availability of TX Text Control 30.0 for all platforms including ASP.NET, Windows Forms, WPF and ActiveX and TX Spell .NET 9.0 for all .NET based platforms.
TX Text Control X19 and TX Spell 8.0 Have Been Released
We are happy to announce the immediate availability of TX Text Control X19 for all platforms including Windows Forms, WPF and ASP.NET and TX Spell .NET 8 for all .NET based platforms.