Products Technologies Demo Docs Blog Support Company

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.

MailMerge: Using the FieldMerged Event to Highlight Table Cells

The TXTextControl.DocumentServer.MailMerge.FieldMerged event returns useful information and options to manipulate the merged content in the TXTextControl.DocumentServer.MailMerge.FieldMergedEventArgs class.

Since version X14 (24.0), the TXTextControl.DocumentServer.MailMerge.FieldMergedEventArgs.TableCell property returns a TXTextControl.TableCell class in case the merge field is positioned inside a table. This can be used to manipulate the table cell based on merged values.

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 TXTextControl.DocumentServer.MailMerge class, attaches the FieldMerged event and calls the TXTextControl.DocumentServer.MailMerge.MergeObjects method to merge a template with the created data object.

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:

Colored table cells

This is just one of many ideas what to do with the flexible reporting framework MailMerge to customize the merging process.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • TXTextControl.DocumentServer.MailMerge Class
  • TXTextControl.DocumentServer.MailMerge.FieldMerged Event
  • TXTextControl.DocumentServer.MailMerge.FieldMergedEventArgs Class
  • TXTextControl.DocumentServer.MailMerge.FieldMergedEventArgs.TableCell Property
  • TXTextControl.DocumentServer.MailMerge.MergeObjects Method
  • TXTextControl.TableCell 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.

See Windows Forms products

Related Posts

ASP.NETWindows FormsWPF

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…


ASP.NETWindows FormsWPF

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…


ActiveXASP.NETWindows Forms

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…


ASP.NETWindows FormsWPF

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…


ASP.NETWindows FormsWPF

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…