MailMerge: Conditional Table Cell Colors using Filter Instructions
The MailMerge class provides an extensible framework to inject custom logic to the merge process. This sample shows how to implement conditional table cell colors.

The TXText
This combination allows creative implementations such as conditional table cell colors based on specific filter instructions.
In the following sample, the table cell Qty should be highlighted red, if quantity is higher than 10, otherwise it should be green.
These conditions can be adjusted in the following dialog:
When merging the document, the conditions are evaluated in the custom events:
The class CellFilterInstructions contains the instructions for the custom filter:
public class CellFilterInstructions
{
public double? CompareValue { get; set; } = null;
public RelationalOperator? Operator { get; set; } = null;
public Color TrueColor { get; set; } = Color.White;
public Color FalseColor { get; set; } = Color.White;
public enum RelationalOperator
{
Equals = 0,
NotEqual,
LessThan,
GreaterThan,
}
}
This class is serialized as JSON and stored in the TXText
// create a new dialog form
frmCellFormat frmCellFormat = new frmCellFormat();
// check, if there are existing filter instructions
CellFilterInstructions cellFilterInstructions =
(CellFilterInstructions)JsonConvert.DeserializeObject(
textControl1.Tables.GetItem().Cells.GetItem().Name,
typeof(CellFilterInstructions));
if (cellFilterInstructions == null)
{
// create new instructions
Color cCellBackColor = textControl1.Tables.GetItem().Cells.GetItem().CellFormat.BackColor;
cellFilterInstructions = new CellFilterInstructions()
{
TrueColor = cCellBackColor,
FalseColor = cCellBackColor
};
}
// show the form
frmCellFormat.CellFilterInstructions = cellFilterInstructions;
if (frmCellFormat.ShowDialog() == DialogResult.OK)
{
// save the instructions in the Name property
textControl1.Tables.GetItem().Cells.GetItem().Name =
JsonConvert.SerializeObject(frmCellFormat.CellFilterInstructions);
}
In the FieldMerging event, the instructions are evaluated and the custom background color is applied to the cell that is returned in the event arguments:
private void MailMerge_FieldMerged(object sender,
TXTextControl.DocumentServer.MailMerge.FieldMergedEventArgs e)
{
// custom field handling
if (e.TableCell == null)
return;
// if TableCell.Name has instructions, create a CellFilterInstructions object
// and evaluate the instructions and set the table cell color
if (e.TableCell.Name != "")
{
CellFilterInstructions instructions =
(CellFilterInstructions)JsonConvert.DeserializeObject(
e.TableCell.Name,
typeof(CellFilterInstructions));
// retrieve the color
Color? color = instructions.GetColor(e.MailMergeFieldAdapter.ApplicationField.Text);
// apply the color
if(color != null)
e.TableCell.CellFormat.BackColor = (Color)color;
}
}
Test this sample on your own and download the GitHub repository.
Also See
This post references the following in the documentation:
- TXText
Control. Document Server. Mail Merge Class - TXText
Control. Document Server. Mail Merge. Field Merged Event - TXText
Control. Table Cell Class - TXText
Control. Table Cell. Name Property
Download and Fork This Sample on GitHub
We proudly host our sample code on github.com/TextControl.
Please fork and contribute.
Requirements for this sample
- Visual Studio 2017 or better
- TX Text Control .NET for Windows Forms (trial sufficient)
ASP.NET
Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
MailMerge: Using Filters to Remove Unwanted Rows
Data in merge blocks can be filtered and sorted. This article explains how to use filters to remove unwanted lines.
Different Ways to Create Documents using Text Control Products
There are many ways to create documents using Text Control products. This article gives an overview of different approaches using our products.
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.
An Ultimate Guide to Mail Merge with MS Word Documents in C#
The MailMerge class provides very effective ways to merge data into MS Word compatible templates. This updated ultimate guide provides an overview of all the important features and functionalities…
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.