You now have more mail merge options than ever! We're enhancing our mail merge functionality in RichEdit to include support for nested master-detail merge operations.
Remember the new DOCVARIABLE fields that we're adding? Well, they're like the MERGEFIELD but you don't need to bind them to a datasource, instead, you calculate their value at runtime. If we combine these with the RichEditDocumentServer, which allows you to access the functionality of the RichEdit control via the API, you can perform a master-detail merge. This is huge! You can create catalogs, header/detail invoices, or even statements!
It works similarly to a basic mail merge. First, you need the templates. For this example, let's create a sheet of customers and orders. First, you would set up the main mail merge template in this manner:

with a master template for the orders like this:

and a detail template to describe the products in each category, as follows:

In the master template, the document variable takes a merge field as a parameters. When you update the master fields during the merge, the CalculateDocumentVariables event is raised, which will allow you to calculate and display the nested variables for each section. Let's go through this bit by bit. :)
First, I added a Merge button to the ribbon, and in the ItemClick event for it, I set the mail merge datasource, make sure to wire up the Calculate Document Variables event, and then merge the original template (reTemplate) and display it in the reResultingDoc RichEditControl.
private void mergebutton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
reTemplate.Options.MailMerge.DataSource = GetDocument();
reResultingDoc.CalculateDocumentVariable += reResultingDoc_CalculateDocumentVariable;
reTemplate.MailMerge(reResultingDoc.Document);
}
This next step loads all the customers into the template. When the mail merge begins, the document variables event is triggered to calculate the "Customers" DOCVARIABLE. Here, we create an instance of the RichEditDocumentServer, and wire the document server's CalculuateDocumentVariables event to a new method for it. We're using the Customer template for the "master", so we set the datasource for this template, and perform a mail merge into the just created document server.
void reResultingDoc_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e) {
if (String.Equals(e.VariableName, "Customers")) {
reCustomer.Options.MailMerge.DataSource = GetCustomers();
RichEditDocumentServer docserver = new RichEditDocumentServer();
docserver.CalculateDocumentVariable += docserver_CalculateDocumentVariable;
reCustomer.MailMerge(docserver);
e.Value = docserver;
e.Handled = true;
}
}
Now, we still need to load the orders, so we repeat the steps: create an instance of the RichEditDocumentServer, set the datasource for the template to use (in this case, the Order template), and mail merge from the RichEditControl which contains the template, into the RichEditDocumentServer that we've just created.
void docserver_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e) {
if (String.Equals(e.VariableName, "Orders")) {
if (e.Arguments.Count > 0) {
RichEditDocumentServer docserverOrders = new RichEditDocumentServer();
reOrder.Options.MailMerge.DataSource = GetOrders(e.Arguments[0].Value);
reOrder.MailMerge(docserverOrders);
e.Value = docserverOrders;
e.Handled = true;
}
}
}
And, our output file:

I can't wait to see what you guys do with this! I know I've been asked about it a bunch of times, and I'm sure you're all pretty excited to see it. :D
For more information, check out the master-detail merge demo in our demo center – it's in the Beta!