Table in Word with Sub-Totals?

I used Words on a recent project with many reports and it worked great.
I had table in a word document wrapped in table start/end and a final total (no sub totals). It was easy to handle a final total. I just placed a row in the MS Word table with field names, then populated the totals by using the Document.MailMerge.Execute(fields,values) with amounts I accumulated and it worked great. This will not work for sub-totals.
My question is this: I have a table and in the data source it may contain 1 or more States. I need to sub-total the amount by State in the table. So in this case I cannot use Document.MailMerge.Execute(fields,values).
How can I best accomplish this task? Will I need to create a data table and populate it from the data source and then in a loop handle the accumulation of totals by state, and when the state breaks to the new state, insert a sub-total row in the data table and then use that data table in the MS Word table start/end? I am wondering if there is an eaiser way to handle this type of control-break sub-total process?

Hi
Thanks for your inquiry. I suppose, you can achieve this using Mail Merge With Nested Regions:
https://docs.aspose.com/words/net/nested-mail-merge-with-regions/
This approach will allow you to generate a separate table for each state and insert a sub-total at the end of each table. Please let me know if such approach is acceptable for you.
Also, it would be great if you attach sample data (in XML format for example), your template and the expected output (you can create it in MS Word). We will check your inputs and try to help you to achieve what you need.
Best regards,

The example you provided looks good.
Questions

  1. It looks like the table within a tablet would work like the Mail Merge?
  2. What happens if the data extends beyond the page, is there a way that the header on the table will repeat as in the example of the Pizza Items ordered?

Thank you

Hi
Thanks for your inquiry.

  1. Not sure, I got your question correctly. When you use nested regions, it is expected that in your data source you have relationship between data in the main region and in the nested region. When you execute mail merge, for each record in the main table Aspose.Words selects and fills the child region data.
  2. Sure, you can achieve this. You should just set “Repeat as header row at the top of each page” option for the header row in your template. In this case the header row will be repeated on each page.

Best regards,

Thanks again…

  1. The choice to use Mail Merge or not from the link you provide. The dataset would have two tables and a relationship between both. Then using MailMerge.Execute with Regions.

Why define {MERGEFIELD TableStart:Orders...} vs. <<TableStart: Orders>>. I have used the latter on many reports. (Image added to post). It seems when MailMerge.Execute is used and the relationship between the tables in the dataset exist, the same result would be produced? In other words the Parent → Detail relationship in printing would work.
https://docs.aspose.com/words/net/nested-mail-merge-with-regions/

Hi Patrick,
Thanks for your inquiry.
Both fields in the image are the same, the left shows how the fields are displayed without field codes on and the right shows the fields with field codes. You can toggle the display of field codes by pressing ALT+F9 inside Microsoft Word.
Thanks,