Posted 14 July 2023, 5:59 am EST
Hello,
I didn’t find a way to hide/delete a table if the data source is empty
Forums Home / Document Solutions / Document Solutions for Word
Posted by: cozzupoli on 14 July 2023, 5:59 am EST
Posted 14 July 2023, 5:59 am EST
Hello,
I didn’t find a way to hide/delete a table if the data source is empty
Posted 17 July 2023, 6:34 am EST
Hi,
Thanks for reaching out to us with your query.
You can achieve this requirement by: (see code snippet)
//Check Data in each tables
foreach (var section in doc.Body.Sections)
{
foreach (var table in section.GetRange().Tables)
{
var isEmpty = true;
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < table.Rows[0].Cells.Count; j++)
{
var par = (table.Rows[i].Cells[j].Children.First() as Paragraph);
var run = par.Children.FirstOrDefault() as Run;
if (run != null)
isEmpty = false;
}
}
if (isEmpty)
tablesToDelete.Add(table);
}
}
// Delete all tables
foreach (var table in tablesToDelete)
{
table.Delete();
}
Please refer to the attached sample for the same: TableDelete.zip
Best Regards,
Nitin