Hi,
I have a question on row grouping or even nested table. In my word template, I have a table with 3 columns. In dataset, the first column will contain a Category Name while the other columns’ rows will have names and ID as sub categories . There will be only 2 sub categories per category. So:
My dataset looks like:
Category: SubCategory: SubCategoryID
Category A SubCat A1 A111
Category A SubCat A2 A222
Category B SubCat B1 B111
Category B SubCat B2 B222
In my template, I have 3 columns. However, I would like to merge the table cells for Column 1 grouped by category. Any ideas?
Hi
Thanks for your request. There is no direct way to merge cells during mail merge, but you can easily emulate this. You should create template like the following:
Category | SubCategory | SubCategoryID |
---|---|---|
«TableStart:Categories»«Category» |
«TableStart:SubCategories»«SubCategory» | «SubCategoryID»«TableEnd:SubCategories» |
---|
«TableEnd:Categories» |
---|
Green table is nested table.
Then you should have two tables in datatset named Categories and SubCategories. I created sample template (see the attachment) and code for you.
// Create test datatable.
DataTable data = new DataTable("SubCategories");
data.Columns.Add("Category");
data.Columns.Add("SubCategory");
data.Columns.Add("SubCategoryID");
// Add some dummy data.
data.Rows.Add(new object[]
{
"Category A",
"SubCat A1",
"A111"
});
data.Rows.Add(new object[]
{
"Category A",
"SubCat A2",
"A222"
});
data.Rows.Add(new object[]
{
"Category B",
"SubCat B1",
"B111"
});
data.Rows.Add(new object[]
{
"Category B",
"SubCat B2",
"B222"
});
// Now let create dataset. We need to have two DataTables, one with categories,
// And another with sub categories (we alredy have this table).
// We can use DataView to get table of categories.
DataView dv = new DataView(data);
DataTable categories = dv.ToTable("Categories", true, new string[]
{
"Category"
});
// Now we can add both tables into DataSet.
DataSet ds = new DataSet();
ds.Tables.Add(categories);
ds.Tables.Add(data);
// Add relationship between tables.
ds.Relations.Add(categories.Columns["Category"], data.Columns["Category"]);
// Datasource is ready and we can execute mail merge.
// Open template.
Document doc = new Document(@"Test001\in.doc");
// Execute mail merge with regions.
doc.MailMerge.ExecuteWithRegions(ds);
// Save the result.
doc.Save(@"Test001\out.doc");
Hope this helps.
Best regards.