Hide whole table if dataset is empty

Posted by: cozzupoli on 13 July 2023, 8:59 pm EST

  • Posted 13 July 2023, 8:59 pm EST

    Hello,

    I didn’t find a way to hide/delete a table if the data source is empty

  • Posted 16 July 2023, 9:34 pm 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

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels