Word formatting issue

I need populate the data from dataset into a word table. I am facing some issues while formatting the output. I am using MailMerge to populate the data from data-table to word table. Below is the sample data.

Data-Table:

Location Name Age
India A 2
India B 4
India C 5
India D 6
India E 7
US F 9
US G 8
US H 10
US I 11

Intended format in word table:

Name Age
India
A 2
B 4
C 5
D 6
E 7
US
F 9
G 8
H 10
I 11

Please help me with the code to get above formatting details.

Regards,
Aniket

Hi Aniket,

Thanks for your inquiry. Sure, you can achieve this by using Nested Mail Merge Regions functionality of Aspose.Words. I have attached sample input/output documents here for your reference. Here is the sample code:

// inside main
DataTable table = GetDataTable();
DataView view = new DataView(table);
DataTable parent = view.ToTable("Parent", true, new string[]
{
    "Location"
});
DataTable child = view.ToTable("Child", true, new string[]
{
    "Location",
    "Name",
    "Age"
});
DataSet ds = new DataSet();
ds.Tables.Add(parent);
ds.Tables.Add(child);
ds.Relations.Add(new DataRelation("rel", parent.Columns[0], child.Columns[0], true));
Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.ExecuteWithRegions(ds);
doc.Save(@"C:\Temp\out.docx");

// helper method
private static DataTable GetDataTable()
{
    DataTable dataTable = new DataTable("table");
    dataTable.Columns.Add(new DataColumn("Location", typeof(string)));
    dataTable.Columns.Add(new DataColumn("Name", typeof(string)));
    dataTable.Columns.Add(new DataColumn("Age", typeof(string)));
    DataRow dataRow = dataTable.NewRow();
    dataRow["Location"] = "India";
    dataRow["Name"] = "A";
    dataRow["Age"] = "2";
    dataTable.Rows.Add(dataRow);
    dataRow = dataTable.NewRow();
    dataRow["Location"] = "India";
    dataRow["Name"] = "B";
    dataRow["Age"] = "4";
    dataTable.Rows.Add(dataRow);
    dataRow = dataTable.NewRow();
    dataRow["Location"] = "India";
    dataRow["Name"] = "C";
    dataRow["Age"] = "5";
    dataTable.Rows.Add(dataRow);
    dataRow = dataTable.NewRow();
    dataRow["Location"] = "India";
    dataRow["Name"] = "D";
    dataRow["Age"] = "6";
    dataTable.Rows.Add(dataRow);
    dataRow = dataTable.NewRow();
    dataRow["Location"] = "India";
    dataRow["Name"] = "E";
    dataRow["Age"] = "7";
    dataTable.Rows.Add(dataRow);
    dataRow = dataTable.NewRow();
    dataRow["Location"] = "US";
    dataRow["Name"] = "F";
    dataRow["Age"] = "9";
    dataTable.Rows.Add(dataRow);
    dataRow = dataTable.NewRow();
    dataRow["Location"] = "US";
    dataRow["Name"] = "G";
    dataRow["Age"] = "8";
    dataTable.Rows.Add(dataRow);
    dataRow = dataTable.NewRow();
    dataRow["Location"] = "US";
    dataRow["Name"] = "H";
    dataRow["Age"] = "10";
    dataTable.Rows.Add(dataRow);
    dataRow = dataTable.NewRow();
    dataRow["Location"] = "US";
    dataRow["Name"] = "I";
    dataRow["Age"] = "11";
    dataTable.Rows.Add(dataRow);
    return dataTable;
}

I hope, this helps.

Best regards,