MailMerge: Using the FieldMerged Event to Highlight Table Cells
The FieldMerged event returns the TableCell, if the field is positioned inside a table. This article shows how to manipulate this cell.

The TXText
Since version X14 (24.0), the TXText
Consider the following data structure and instance that is used to merge a template:
public class Report
{
public Row[] Rows { get; set; }
}
public class Row
{
public int Value { get; set; }
public string Name { get; set; }
}
Report report = new Report()
{
Rows = new Row[] {
new Row() { Name = "Row0", Value = 10 },
new Row() { Name = "Row1", Value = 100 },
new Row() { Name = "Row2", Value = 200 },
new Row() { Name = "Row3", Value = 300 },
new Row() { Name = "Row4", Value = 400 },
new Row() { Name = "Row5", Value = 500 },
new Row() { Name = "Row6", Value = 600 },
new Row() { Name = "Row7", Value = 700 },
}
};
reports = new List<Report>() { report };
The following code creates a new instance of the reporting engine TXText
using (MailMerge mailMerge = new MailMerge())
{
mailMerge.TextComponent = textControl1;
mailMerge.FieldMerged += mailMerge_FieldMerged;
mailMerge.MergeObjects(reports);
}
In the FieldMerged event, the field value is parsed and mapped to a color based on the value range. Accordingly, the background color of the outer table cell is highlighted.
private void mailMerge_FieldMerged(object sender, MailMerge.FieldMergedEventArgs e)
{
if (e.TableCell == null)
return;
if (e.MailMergeFieldAdapter.TypeName == MergeField.TYPE_NAME)
{
MergeField mergeField = e.MailMergeFieldAdapter as MergeField;
Color cellColor;
int value;
if (int.TryParse(mergeField.Text, out value) == true)
{
if (value >= 700)
cellColor = Color.Red;
else if (value >= 600)
cellColor = Color.Orange;
else if (value >= 500)
cellColor = Color.Yellow;
else if (value >= 400)
cellColor = Color.Green;
else if (value >= 300)
cellColor = Color.Blue;
else if (value >= 200)
cellColor = Color.Indigo;
else if (value >= 100)
cellColor = Color.Violet;
else
cellColor = e.TableCell.CellFormat.BackColor;
e.TableCell.CellFormat.BackColor = cellColor;
}
}
}
The following screenshot shows the results of this merge process:
This is just one of many ideas what to do with the flexible reporting framework MailMerge to customize the merging process.
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. Document Server. Mail Merge. Field Merged Event Args Class - TXText
Control. Document Server. Mail Merge. Field Merged Event Args. Table Cell Property - TXText
Control. Document Server. Mail Merge. Merge Objects Method - TXText
Control. Table Cell Class
Windows Forms
Text Control combines the power of a reporting tool and an easy-to-use WYSIWYG word processor - fully programmable and embeddable in your Windows Forms application. TX Text Control .NET for Windows Forms is a royalty-free, fully programmable rich edit control that offers developers a broad range of word processing features in a reusable component for Visual Studio.
Related Posts
TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version
TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…
TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version
TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…
Service Pack Releases: What's New in TX Text Control 33.0 SP1 and 32.0 SP5
TX Text Control 33.0 Service Pack 1 and TX Text Control 32.0 Service Pack 5 have been released, providing important updates and bug fixes across platforms. These service packs improve the…
The Wait is Over: TX Text Control for Linux is Officially Here
We are very excited to announce the release of TX Text Control 33.0 which includes the long awaited Linux version of TX Text Control. This version allows you to integrate TX Text Control into your…
Full .NET 9 Support in Text Control .NET Components for ASP.NET Core,…
.NET 9 will be launched tomorrow, November 12, at the .NET Conf 2024 with updates to cloud capabilities, security, and performance. TX Text Control .NET components are fully compatible with .NET 9…