TX Text Control .NET for WPF

DataSets and DataTables

Topic

A DataTable filled from a query, or a DataSet that holds several of them with their relations, is a ready-made data source for MailMerge. The column names become field names, and the relations decide which rows fill the merge blocks of each master record. Below, you merge a single table and related tables in a WPF window, make the tables available in the Reporting tab and let users edit relations there.

The examples turn the invoice of the Mail Merge tutorials into two tables: Invoice, with the columns Company, Name and InvoiceNumber, and Items, which fills the merge block Items.

Merge a DataTable

Merge takes a DataTable, uses its column names as field names and merges the template once for each row. Its second parameter, append, combines the results into one document when it is true and keeps them separate when it is false.

Offer the Tables in the Reporting Tab

The DataSourceManager of the WPF Reporting tab loads a DataSet with its relations through LoadDataSet, and a single table through LoadDataTable. Load the data when the TextControl has loaded, in a handler connected with Loaded="textControl1_Loaded" on the TextControl element:

private void textControl1_Loaded(object sender, RoutedEventArgs e)
{
    ribbonReportingTab1.DataSourceManager.LoadDataSet(CreateInvoiceData());
}

Select Master Table then chooses the table whose columns Insert Merge Field lists, and Insert Merge Block offers the tables related to it. In code, PossibleMergeBlockTables returns these tables for the current master table.

Edit Relations in the Designer

Edit Data Relations in the Data Source group of the Reporting tab lets users add relations the loaded data lacks, by choosing a main table, a child table and the columns that connect them. The dialog box behind the button is EditDataRelationsDialog of the TXTextControl.DocumentServer.WPF namespace, a WPF Window that your code can open too. DataRelations lists the relations afterward:

using System.Diagnostics;
using System.Windows;
using TXTextControl.DocumentServer.DataSources;
using TXTextControl.DocumentServer.WPF;
private void relationsButton_Click(object sender, RoutedEventArgs e)
{
    DataSourceManager dataSourceManager = ribbonReportingTab1.DataSourceManager;

    // A modal WPF window, owned by the main window
    EditDataRelationsDialog dialog = new EditDataRelationsDialog(dataSourceManager) { Owner = this };
    dialog.ShowDialog();

    foreach (DataRelationInfo relation in dataSourceManager.DataRelations)
    {
        Debug.WriteLine($"{relation.ParentTableName} -> {relation.ChildTableName}");
    }
}

The relations created this way belong to the DataSourceManager, the data of the designer. A WPF application can't save them in a data source configuration file, as Data Source Configuration Files explains; add the same relations to the DataSet your code merges.

Replace MergeBlocks in Older Code

Older code may call MergeBlocks, which merges the blocks of a template with the tables of a DataSet named like the blocks. The method is obsolete: since version 20.0, Merge fills blocks from the related tables of the master table. Deprecated APIs lists it with its replacement.

Next Steps