Show "No data available" when there is no data in table

I have a simple table in my word document:

that I'm filling using "document.MailMerge.ExecuteGroup(table);" (as shown in docs: https://help.syncfusion.com/file-formats/docio/mail-merge/mail-merge-for-group)
This works great if I have rows in DataTable, but sometimes the DataTable is empty and I get a table that has only header.
Instead, I'd like to get this:


I use:
document.MailMerge.RemoveEmptyGroup = true;
document.MailMerge.RemoveEmptyParagraphs = true;

but If I remove that I get empty line.

Can this be done via for example BeforeClearGroupField?
Idea is to merge all fields of table in row, center text and add "No data available".

Best regards,
Tomasz


7 Replies

MR Manikandan Ravichandran Syncfusion Team April 6, 2020 12:51 PM UTC

Hi Tomasz,

Thank you for contacting Syncfusion support.

Currently we are preparing the sample for your requirement, we will update the details shortly.

Please let us know if you have any other questions.

Regards,
Manikandan Ravichandran



TJ Tomasz Jagusz April 6, 2020 12:53 PM UTC

Hi Manikandan,

thank you for the update :)



MR Manikandan Ravichandran Syncfusion Team April 6, 2020 03:07 PM UTC

Hi Tomasz,

Thank you for your patience.

For your requirement with “Preserve No record found text when there is no record in the dtata”, we have prepared the sample and it can be downloaded from the below link.
https://www.syncfusion.com/downloads/support/forum/152998/ze/WinFormsAPP-1218055002

In this sample we have done the followings.
1. Load the template document.
2. Initialize BeforeClearGroupEvent and MergeFieldEvent.
3. Execute the mail merge using ExecuteGroup and Execute APIs.
4. Add alternate value (“No Record Found”) for fields which doesn’t have any record in the BeforeClearGroupEvent
5. Find the “No Record Found” text and get the owner table of the current field.
6. Apply horizontal merge for the particular row of the table.
7. Save the document as PDF and close

Please let us know if you have any other questions.

Regards,
Manikandan Ravichandran
 



TJ Tomasz Jagusz April 6, 2020 07:22 PM UTC

Hi Manikandan,

the sample you provided is almost perfect, but I'm looking for a more generic solution.
In the solution, you have provided (which works great BTW), I need to swap data source of the mail merge.
To do this I need to create a list of objects that has an object with the same field as my original data table - my data table has "Podmiot" field, and OrderDetails has "Podmiot" field.
Would it be possible to not create a new object or do it dynamically?

I've tried creating DataTablein this way:

 var dt = new DataTable();
 dt.Columns.Add(args.FieldNames[0]);
 var row = dt.NewRow();
 row[0] = "No Record Found";
 dt.Rows.Add(row);

but I must return IEnumerable instead of DataTable.

EDIT:
I was able to get the results I need using below code:

private static void MailMerge_BeforeClearGroupField(object sender, BeforeClearGroupFieldEventArgs args)
{
    Debug.WriteLine(args);
    var alt = new ExpandoObject();
    var expandoDict = (IDictionary) alt;
    expandoDict.Add(args.FieldNames[0], "No Record Found");
    args.AlternateValues = new List<object> {alt};
}


TJ Tomasz Jagusz April 6, 2020 07:29 PM UTC

There is still something wrong.
When I add extra fields from my Primary DataTable as shown below:

this field is added to group and shown in MailMerge_BeforeClearGroupField, although it doesn't belong to secondary and to template row:


I'm attaching a modified template, the rest can stay the same.
Could you please take a look?

Best regards,
Tomasz

Attachment: Template_v2_2e0a560.zip


MR Manikandan Ravichandran Syncfusion Team April 7, 2020 04:26 PM UTC

Hi Tomasz,

Thank you for contacting Syncfusion support.

We can reproduce the mentioned “Unwanted Merge fields added in the Merge group fields collection” at our end. We suspect this could be a defect and We will update you with more details on this on 9th April 2020.

Please let us know if you have any other questions.

Regards,
Manikandan Ravichandran



Loader.
Up arrow icon