# Mail Merge: Conditional Data Shaping in Merge Blocks

> Rows of data in merge blocks can be sorted, filtered, and rendered conditionally. This example shows how to set the required JSON data on a merge block to conditionally render child blocks.

- **Author:** Bjoern Meyer
- **Published:** 2023-05-03
- **Modified:** 2025-11-16
- **Description:** Rows of data in merge blocks can be sorted, filtered, and rendered conditionally. This example shows how to set the required JSON data on a merge block to conditionally render child blocks.
- **3 min read** (413 words)
- **Tags:**
  - ASP.NET
  - MailMerge
  - Merge Block
  - Conditional
- **Web URL:** https://www.textcontrol.com/blog/2023/05/03/mail-merge-conditional-data-shaping-in-merge-blocks/
- **LLMs URL:** https://www.textcontrol.com/blog/2023/05/03/mail-merge-conditional-data-shaping-in-merge-blocks/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2023/05/03/mail-merge-conditional-data-shaping-in-merge-blocks/llms-full.txt

---

Rows of data in merge blocks can be sorted, filtered, and rendered conditionally. This example shows how to set the required JSON data on a merge block to conditionally render child blocks.

### Template with Merge Blocks

Consider the following template, which consists of a merge block named *lineItems* with a nested merge block *details*.

![Conditional Merge Blocks](https://s1-www.textcontrol.com/assets/dist/blog/2023/05/03/a/assets/template.webp "Conditional Merge Blocks")

### JSON Data Source

The data source is a JSON object with two simple product line items.

```
[
  {
    "title": "Report Title",
    "lineItems": [
      {
        "name": "product 1",
        "price": 200,
        "details": [
          {
            "shortDescription": "short description 1",
            "description": "description 1"

          }
        ]
      },
      {
        "name": "product 2",
        "price": 100,
        "details": [
          {
            "shortDescription": "short description 2",
            "description": "description 2"
          }
        ]
      }
    ]
  }
]
```

The idea of the conditional data shaping is to render a child table (*details*) only when certain conditions of the parent table are met. The conditions can be defined in the form of a JSON object that is stored in the SubTextPart.Data property.

The following data shaping information is defined in the template by default:

```
{
   "Name":"details",
   "DataShapingInfo":{
      "BlockMergingCondition":[
         {
            "ColumnName":"price",
            "ComparisonOperator":0,
            "CompareTo":"200",
            "LogicalOperator":0,
            "Culture":""
         }
      ],
      "Filters":[],
      "SortingInstructions":[]
   }
}
```

The condition is basically met when the parent column name *price* equals *200*. In this case, the child merge block is rendered and the nested merge block of the second data row is not rendered.

![Conditional Merge Blocks](https://s1-www.textcontrol.com/assets/dist/blog/2023/05/03/a/assets/template1.webp "Conditional Merge Blocks")

### Demo Application

This article describes a sample that is part of the technical live demos.

> **Live Demo**
> 
> Try this demo for yourself by launching our technical demos.
> 
> [ Launch Demo ](https://demos.textcontrol.com/chapter/topic/RichTextEditor/MailMergeConditions)

In the demo, you can now play with the conditions by editing the JSON directly. Set the *CompareTo* value to *100* to render the nested merge block for the second data row and click *Set Condition* to apply the data shaping information.

[![Conditional Merge Blocks](https://s1-www.textcontrol.com/assets/dist/blog/2023/05/03/a/assets/template2.webp "Conditional Merge Blocks")](https://s1-www.textcontrol.com/assets/dist/blog/2023/05/03/a/assets/template2.webp)

The following comparison values are supported:

| Operator | Description |
|---|---|
| 0 | Equal to |
| 1 | Greater than |
| 2 | Greater than or equal |
| 3 | Less than |
| 4 | Less than or equal |
| 5 | Not equal to |
| 6 | Is blank |
| 8 | Is not blank |

When you click the *Set Conditions* button, JavaScript is used to apply the JSON data to the merge block *details*:

```
function setCondition() {

    TXTextControl.subTextParts.forEach(function (stp) {
        stp.getName(function (name) {
            if (name == "txmb_details") {
                stp.setData(editor.getValue(), function () {
                    $("#ribbonTabReports_btnPreviewMergeResults").click();
                });
            }
        })
    })
}
```

---

## 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

- [Mail Merge: Inserting Merge Blocks using the DataSourceManager in C#](https://www.textcontrol.com/blog/2024/10/02/mail-merge-inserting-merge-blocks-using-the-datasourcemanager-in-csharp/llms.txt)
- [Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX Templates](https://www.textcontrol.com/blog/2025/05/27/use-mailmerge-in-dotnet-on-linux-to-generate-pixel-perfect-pdfs-from-docx-templates/llms.txt)
- [Generating Dynamic NDAs Using TX Text Control MailMerge in C# .NET](https://www.textcontrol.com/blog/2025/04/08/generating-dynamic-ndas-using-tx-text-control-mailmerge-in-csharp-dotnet/llms.txt)
- [Designing a Maintainable PDF Generation Web API in ASP.NET Core (Linux) C# with Clean Architecture and TX Text Control](https://www.textcontrol.com/blog/2025/03/27/designing-a-maintainable-pdf-generation-web-api-in-asp-net-core-linux-c-sharp-with-clean-architecture-and-tx-text-control/llms.txt)
- [Getting Started: ServerTextControl and MailMerge in a .NET 8 Console Application on Linux with Docker and WSL](https://www.textcontrol.com/blog/2025/03/12/getting-started-servertextcontrol-and-mailmerge-in-a-net-8-console-application-on-linux-with-docker-and-wsl/llms.txt)
- [Manipulating Table Cells During the MailMerge Process in .NET C#](https://www.textcontrol.com/blog/2024/12/03/manipulating-table-cells-during-the-mailmerge-process-in-net-csharp/llms.txt)
- [When to Generate Documents Server-Side Instead of Client-Side: A Focus on Data Security](https://www.textcontrol.com/blog/2024/10/04/when-to-generate-documents-server-side-instead-of-client-side-a-focus-on-data-security/llms.txt)
- [Creating Advanced Tables in PDF and DOCX Documents with C#](https://www.textcontrol.com/blog/2024/09/30/creating-advanced-tables-in-pdf-and-docx-documents-with-csharp/llms.txt)
- [Designing the Perfect Contract Template for MailMerge in C#](https://www.textcontrol.com/blog/2024/09/26/designing-the-perfect-contract-template-for-mailmerge-in-csharp/llms.txt)
- [Video Tutorial: Creating a MailMerge Template and JSON Data Structure](https://www.textcontrol.com/blog/2024/08/16/video-tutorial-creating-a-mailmerge-template-and-json-data-structure/llms.txt)
- [Getting Started Video Tutorial: How to use the MailMerge and ServerTextControl Classes in ASP.NET Core C#](https://www.textcontrol.com/blog/2024/08/05/getting-started-video-tutorial-how-to-use-the-mailmerge-and-servertextcontrol-classes-in-asp-net-core-c/llms.txt)
- [Getting Started Videos: New Text Control YouTube Channel](https://www.textcontrol.com/blog/2024/08/02/getting-started-videos-new-text-control-youtube-channel/llms.txt)
- [Best Practices for Mail Merge and Form Field Processing in ASP.NET Core C# Applications](https://www.textcontrol.com/blog/2024/07/30/best-practices-for-mail-merge-and-form-field-processing-in-asp-net-core-csharp-applications/llms.txt)
- [Advantages of Flow Type Layout Reporting vs. Banded Reporting or PDF Template Engines in .NET C#](https://www.textcontrol.com/blog/2024/07/29/advantages-of-flow-type-layout-reporting-vs-banded-reporting-or-pdf-template-engines-in-net-c-sharp/llms.txt)
- [Designing a MailMerge Web API Endpoint with ASP.NET Core in C#](https://www.textcontrol.com/blog/2024/07/12/designing-a-mailmerge-web-api-endpoint-with-asp-net-core-in-c-sharp/llms.txt)
- [Enhancing Documents with QR Codes and Barcodes in .NET C#: A Comprehensive Guide](https://www.textcontrol.com/blog/2024/07/11/enhancing-documents-with-qr-codes-and-barcodes-in-net-csharp-a-comprehensive-guide/llms.txt)
- [Document Automation 101: Leveraging TX Text Control for Business Efficiency in .NET C# Applications](https://www.textcontrol.com/blog/2024/07/09/document-automation-101-leveraging-tx-text-control-for-business-efficiency-in-net-c-applications/llms.txt)
- [Mail Merge: The Pre-Shaped Data Concept Explained](https://www.textcontrol.com/blog/2024/05/30/mail-merge-the-pre-shaped-data-concept-explained/llms.txt)
- [Creating Valid XRechnung / ZUGFeRD Invoices with ASP.NET Core C#](https://www.textcontrol.com/blog/2023/12/28/creating-valid-xrechnung-zugferd-invoices-with-asp-net-core-csharp/llms.txt)
- [Merging Templates with MailMerge with Different Merge Field Settings in C#](https://www.textcontrol.com/blog/2023/12/16/merging-templates-with-mailmerge-with-different-merge-field-settings/llms.txt)
- [Store and Merge Templates in a Database using LiteDB and ASP.NET Core C#](https://www.textcontrol.com/blog/2023/11/23/store-and-merge-templates-in-a-database-using-litedb-and-aspnet-core-csharp/llms.txt)
- [How to Mail Merge MS Word DOCX Documents in ASP.NET Core C#](https://www.textcontrol.com/blog/2023/10/16/how-to-mail-merge-ms-word-docx-documents-in-aspnet-core-csharp/llms.txt)
- [Use Case: Blood Pressure Report with Charts in C#](https://www.textcontrol.com/blog/2023/04/27/use-case-blood-pressure-report-with-charts-in-csharp/llms.txt)
- [MailMerge Class Performance Benchmark](https://www.textcontrol.com/blog/2023/04/21/mailmerge-performance-benchmark/llms.txt)
- [MailMerge: Working with Image Placeholders](https://www.textcontrol.com/blog/2022/12/22/mailmerge-working-with-image-placeholders/llms.txt)
