# Populate a Table with Data using Nested Mail Merge Functionality

## Environment

| Version    | Product            | Author                                                                          |
| ---------- | ------------------ | ------------------------------------------------------------------------------- |
| 2024.1.124 | RadWordsProcessing | [Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova) |

## Description

Learn how to generate a Word (.DOCX) document that contains a [Table](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/model/table) with a header row and an item row merge field. When you pass a collection of records, the merge operation automatically creates and populates the data rows.

## Solution

To achieve this result, use the [Nested Mail Merge](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/mail-merge) functionality that [RadWordsProcessing](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/overview) offers and populate the data rows automatically.

The following code snippet demonstrates the approach:

```csharp
internal class Program
{
    static void Main(string[] args)
    {
        List<Player> players = GetPlayers();
        List<PlayersHolder> playersHolders = new List<PlayersHolder>() { new PlayersHolder(players) };
        RadFlowDocument document = new RadFlowDocument();
        RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);

        Table table = editor.InsertTable(2, 2);
        
        table.Rows[0].Cells[0].Blocks.AddParagraph().Inlines.AddRun("First Name");
        table.Rows[0].Cells[1].Blocks.AddParagraph().Inlines.AddRun("Last Name");
        TableRow row = table.Rows[1];
        var firstNameParagraph = table.Rows[1].Cells[0].Blocks.AddParagraph();
        editor.MoveToParagraphStart(firstNameParagraph);
        editor.InsertField("MERGEFIELD TableStart:Players", "");
        editor.InsertField("MERGEFIELD FirstName", "");

        var lastNameParagraph = table.Rows[1].Cells[1].Blocks.AddParagraph();
        editor.MoveToParagraphStart(lastNameParagraph);
        editor.InsertField("MERGEFIELD LastName", "");
        editor.InsertField("MERGEFIELD TableEnd:Players", "");

        table.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100);
        Border border = new Border(1, BorderStyle.Single, new ThemableColor(Colors.Black));
        table.Borders = new TableBorders(border);

        editor.MoveToTableEnd(table);

        editor.InsertParagraph();
        RadFlowDocument mergedDocument = document.MailMerge(playersHolders);
        string resultFileNameDocx = "merged.docx";
        File.Delete(resultFileNameDocx);
        DocxFormatProvider provider = new DocxFormatProvider();
        using (var output = File.OpenWrite(resultFileNameDocx))
        {
            provider.Export(mergedDocument, output);
        }
        Process.Start(resultFileNameDocx);
    }   
    
    public static List<Player> GetPlayers()
    {
        var Players = new List<Player>();

        Players.Add(new Player() { FirstName = "John", LastName = "Baker" });
        Players.Add(new Player() { FirstName = "Sam ", LastName = "Wayne" });
        Players.Add(new Player() { FirstName = "Patrick", LastName = "Gibbs" });
        Players.Add(new Player() { FirstName = "Oscar", LastName = "Stevens" });
        Players.Add(new Player() { FirstName = "Larry", LastName = "Vodden" });

        return Players;
    }
}

public class PlayersHolder
{
    public PlayersHolder(List<Player> players)
    {
        Players = players;
    }

    public List<Player> Players { get; set; }
}

public class Player {
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
```

This code snippet produces the desired result and merges the data into the table in the Word document.

![Populate Table Data](https://www.telerik.com/document-processing-libraries/documentation/assets/5751fbe5cc532d030f1fea39c05622bc/populate-table-data-mail-merge.png)

If you skip the `MailMerge` step, the following template is produced:

![Populate Table Data](https://www.telerik.com/document-processing-libraries/documentation/assets/31a0b41787178f9d8520c8fa1dfb09fe/populate-table-data-mail-merge-original.png)

## See Also

* [Nested Mail Merge](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/mail-merge)
* [WordsProcessing Table Model](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/model/table)
