# MailMerge: Rendering Conditional Table Rows

> The MailMerge class supports repeating merge blocks that are repeated based on the given data rows. Sub-blocks can be rendered conditionally based on value comparisons of the parent data table. This article shows how to add such a condition.

- **Author:** Bjoern Meyer
- **Published:** 2022-02-17
- **Modified:** 2025-11-16
- **Description:** The MailMerge class supports repeating merge blocks that are repeated based on the given data rows. Sub-blocks can be rendered conditionally based on value comparisons of the parent data table. This article shows how to add such a condition.
- **3 min read** (414 words)
- **Tags:**
  - ASP.NET
  - ASP.NET Core
  - Mail Merge
  - Windows Forms
  - WPF
- **Web URL:** https://www.textcontrol.com/blog/2022/02/17/mailmerge-rendering-conditional-table-rows/
- **LLMs URL:** https://www.textcontrol.com/blog/2022/02/17/mailmerge-rendering-conditional-table-rows/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2022/02/17/mailmerge-rendering-conditional-table-rows/llms-full.txt

---

Typically, a merge block is used to repeat content such as table rows or paragraphs. Another purpose of a merge block is to control the rendering of a complete sub-block such as a table row. Those sub-blocks can be rendered conditionally based on value comparisons of the parent data table.

Consider the following JSON data that is merged into the template that is shown further below:

```
[
  {
    "order_id": 123,
    "products": [
      {
        "name": "Product A",
        "price": 123.12,
        "available": false,
        "outofstock": [
          {
            "leadtime": 5
          }
        ]
      },
      {
        "name": "Product B",
        "price": 223.00,
        "available": false,
        "outofstock": [
          {
            "leadtime": 10
          }
        ]
      },
      {
        "name": "Product C",
        "price": 555.00,
        "available": true,
        "outofstock": [
          {
            "leadtime": 7
          }
        ]
      }
    ]
  }
]
```

The following screenshot shows the template with 2 nested merge blocks that are highlighted in blue and purple.

![Merge Block Template](https://s1-www.textcontrol.com/assets/dist/blog/2022/02/17/a/assets/template.webp "Merge Block Template")

The blue merge block shall be rendered in case the *available* value equals *false*. Otherwise the complete merge block should be removed.

Based on the above JSON data, the following result should be rendered:

![Merge Block Template](https://s1-www.textcontrol.com/assets/dist/blog/2022/02/17/a/assets/results.webp "Merge Block Template")

The *outofstock* merge block is rendered for the first 2 rows and not rendered for the last row.

To show how to add the conditions programmatically, the [template](https://s1-www.textcontrol.com/assets/dist/blog/2022/02/17/a/assets/template.tx) contains both merge blocks without the condition. The following code shows how to add the condition to the merge block *outofstock*:

```
// load the template
textControl1.Load("template.tx", TXTextControl.StreamType.InternalUnicodeFormat);

// get the "outofstock" SubTextPart
SubTextPart subTextPart = textControl1.SubTextParts.GetItem("txmb_outofstock");

// create a MergeBlockInfo object
MergeBlockInfo mergeBlockInfo = new MergeBlockInfo(subTextPart);

// add the condition
mergeBlockInfo.BlockMergingCondition =
  new List<TXTextControl.DocumentServer.DataShaping.FilterInstruction>()
  {
       new TXTextControl.DocumentServer.DataShaping.FilterInstruction(
          "available",
          TXTextControl.DocumentServer.DataShaping.RelationalOperator.Equals,
          false
          )
  };

// store the condition in the SubTextPart
mergeBlockInfo.Apply();
```

The MergeBlockInfo class can be initialized with any merge block SubTextPart that is part of the template. Each merge block *SubTextPart* has the prefix *txmb\_*, so that the *outofstock* merge block has the name *txmb\_outofstock*. Using the BlockMergingCondition property, a new FilterInstruction is added.

Finally, the settings are applied and stored in the *SubTextPart*.

These conditions are persistently stored when the template is saved and are used automatically by the MailMerge class when merging the template with the JSON data:

```
string data = System.IO.File.ReadAllText("data.json");

using (MailMerge mm = new MailMerge()) {
  mm.TextComponent = textControl1;
  mm.MergeJsonData(data);
}
```

Additionally, the merge field *leadtime* of the conditional block is populated with values from the *outofstock* table.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Merging Form Fields using the MailMerge Class](https://www.textcontrol.com/blog/2022/02/21/merging-form-fields-using-the-mailmerge-class/llms.txt)
- [TXTextControl.Markdown.Core 34.1.0-beta: Work with Full Documents, Selection, and SubTextParts](https://www.textcontrol.com/blog/2026/04/14/txtextcontrol-markdown-core-34-1-0-beta-work-with-full-documents-selection-and-subtextparts/llms.txt)
- [TX Text Control 34.0 SP2 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2026/02/18/tx-text-control-34-0-sp2-is-now-available/llms.txt)
- [TX Text Control 34.0 SP1 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2025/12/03/tx-text-control-34-0-sp1-is-now-available/llms.txt)
- [Introducing TX Text Control 34.0: Your Next Leap in Document Processing](https://www.textcontrol.com/blog/2025/11/10/introducing-tx-text-control-34-0-your-next-leap-in-document-processing/llms.txt)
- [Sneak Peek: TX Text Control 34.0 Coming November 2025](https://www.textcontrol.com/blog/2025/10/02/sneak-peek-tx-text-control-34-0-coming-november-2025/llms.txt)
- [TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2025/08/14/tx-text-control-33-0-sp3-is-now-available/llms.txt)
- [TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2025/06/18/tx-text-control-33-0-sp2-is-now-available/llms.txt)
- [Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format](https://www.textcontrol.com/blog/2025/05/16/document-lifecycle-optimization-leveraging-tx-text-controls-internal-format/llms.txt)
- [Expert Implementation Services for Legacy System Modernization](https://www.textcontrol.com/blog/2025/05/07/expert-implementation-services-for-legacy-system-modernization/llms.txt)
- [Service Pack Releases: What's New in TX Text Control 33.0 SP1 and 32.0 SP5](https://www.textcontrol.com/blog/2025/05/07/service-pack-releases-whats-new-in-tx-text-control-33-0-sp1-and-32-0-sp5/llms.txt)
- [Top 5 Real-World Applications for TX Text Control Document Processing Libraries](https://www.textcontrol.com/blog/2025/04/01/top-5-real-world-applications-for-tx-text-control-document-processing-libraries/llms.txt)
- [DWX Developer Week Moves to Mannheim - And Text Control Is on Board!](https://www.textcontrol.com/blog/2025/03/19/dwx-developer-week-moves-to-mannheim-and-tx-text-control-is-on-board/llms.txt)
- [The Wait is Over: TX Text Control for Linux is Officially Here](https://www.textcontrol.com/blog/2025/03/12/the-wait-is-over-tx-text-control-for-linux-is-officially-here/llms.txt)
- [Full .NET 9 Support in Text Control .NET Components for ASP.NET Core, Windows Forms, and WPF](https://www.textcontrol.com/blog/2024/11/11/full-net-9-support-in-text-control-net-components-for-asp-net-core-windows-forms-and-wpf/llms.txt)
- [Toggle Field Codes in TX Text Control](https://www.textcontrol.com/blog/2024/11/07/toggle-field-codes-in-tx-text-control/llms.txt)
- [Inserting MergeBlocks with the DataSourceManager and Applying Table Styles in C#](https://www.textcontrol.com/blog/2024/04/09/inserting-mergeblocks-with-the-datasourcemanager-and-applying-table-styles-in-csharp/llms.txt)
- [Sneak Peek 32.0: Modifying the Normal Stylesheet](https://www.textcontrol.com/blog/2023/07/04/sneak-peek-320-modifying-the-normal-stylesheet/llms.txt)
- [An Ultimate Guide to Mail Merge with MS Word Documents in C#](https://www.textcontrol.com/blog/2023/06/07/an-ultimate-guide-to-mail-merge-with-ms-word-documents-in-csharp/llms.txt)
- [Mail Merge with MS Word Documents in C# - An Ultimate Guide](https://www.textcontrol.com/blog/2022/05/25/mail-merge-with-ms-word-documents-in-csharp-an-ultimate-guide/llms.txt)
- [Combining MailMerge and Table of Contents](https://www.textcontrol.com/blog/2022/03/22/combining-mailmerge-and-table-of-contents/llms.txt)
- [Version 30.0: Changes in Licenses.licx](https://www.textcontrol.com/blog/2021/12/02/version-30-changes-in-licenses-licx/llms.txt)
- [MailMerge: Conditional Table Cell Colors using Filter Instructions](https://www.textcontrol.com/blog/2019/06/06/mailmerge-conditional-table-cell-colors-using-filter-instructions/llms.txt)
- [MailMerge: Using Filters to Remove Unwanted Rows](https://www.textcontrol.com/blog/2019/06/04/mailmerge-using-filters-to-remove-unwanted-rows/llms.txt)
- [Different Ways to Create Documents using Text Control Products](https://www.textcontrol.com/blog/2017/10/17/ways-to-create-documents-using-text-control-products/llms.txt)
