Nesting Multiple Data Tables into One, Remove Empty Regions Paragraphs with Start End Fields in Word Document C# .NET

Hi, I need to nest data tables, however, when doing this each row in the parent table is generating a new table and I need it to only generate one table.

My base table called HOSPITAL has a relation HOSPITAL_MSG, as seen below.

HosTable = dataSet.Tables[0];
HosTableMsg = dataSet.Tables[1];

DataRelation hosRelation;
DataColumn[] master1 = new DataColumn[]
{
    HosTable.Columns["ClaimNo"], HosTable.Columns["ClaimLineNo"]
};
DataColumn[] slave1 = new DataColumn[]
{
    HosTableMsg.Columns["ClaimNo"], HosTableMsg.Columns["ClaimLineNo"]
};
hosRelation = new DataRelation("hosRelation", master1, slave1);
dataSet.Relations.Add(hosRelation);

This relation is working fine. I’ve used your RemoveEmptyRegions solution with version 9.3 to remove any empty StartTable/EndTable and mergefields that haven’t been populated by the inner table, and that also works fine.

I believe to do what I’m after I’d need the StartTable/EndTable to be in a row, (currently for the parent table they are outside of the table) and then the child table needs to somehow sit inside of the StartTable/Endtable for the parent table, however, I can’t see that this is possible.

I’ve attached a sample of my document template, and the generated results. I’ve then attached a copy of the document as I’d like to see it. In this example, I’ve manually mapped the lines data table and created the rows and inserted them in the correct location, however, this isn’t very elegant as there are no merge fields defined on the document for the child tables rows.

If you need further info, please contact me.

Hi Nolan,
Thanks for your inquiry.
The way you are trying to use the nested mail merge regions to achieve the output is the best way to go about it. In the future we will add more flexible behaviour to the regions but for now this is the easiest way to go about it.
From what it appears the only difference between the current and expected output is the empty paragraphs caused by the region markers which separate the rows. You can try using this code below to remove these which then result in the tables being combined into “one”.

dataSet.Relations.Add(hosRelation);
doc.MailMerge.FieldMergingCallback = new HandleRemovingMailMergeEmptyParagraphs();
doc.MailMerge.ExecuteWithRegions(dataSet);
doc.Accept(new HandleRemovingMailMergeEmptyParagraphs(false));
public class HandleRemovingMailMergeEmptyParagraphs: IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        // We are interested only in fields who are a part of regions
        if (!string.IsNullOrEmpty(args.TableName))
        {
            // Let's only look at fields that are insides tables in our case.
            Table parentTable = (Table) args.Field.Start.GetAncestor(NodeType.Table);
            if (parentTable != null)
            {
                // Find the previous paragraph, in this case this should be where the region marker is located.
                CompositeNode prevSibling = (CompositeNode) parentTable.PreviousSibling;
                // If it's empty remove it.
                if (prevSibling != null && !prevSibling.HasChildNodes)
                    prevSibling.Remove();
            }
        }
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do Nothing.
    }
}

Thanks,

The issues you have found earlier (filed as 17666) have been fixed in this update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(5)

This feature is now available in this .NET update and in this Java update. You can now avoid empty paragraphs in repeating regions in this version by setting RemoveEmptyParagraphs to true.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(3)