Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue May 23, 2023 2:52 am

Hi, i need anyone help. it won't delete the rows, its only clear the rows. I attached sample output

for (int i = 0; i < table.Rows.Count; i++)
{
bool shouldDelete = true;

TableCell firstCell = table.Rows[i].Cells[0];
TableCell secondCell = table.Rows[i].Cells[1];

for (int p = 0; p < firstCell.Paragraphs.Count; p++)
{
if (string.IsNullOrEmpty(firstCell.Paragraphs[p].Text.Trim()) == false)
{
shouldDelete = false;
break;
}
}

if (shouldDelete)
{
table.Rows.RemoveAt(i);
}
}

ariesphire
 
Posts: 1
Joined: Tue May 23, 2023 2:47 am

Tue May 23, 2023 6:59 am

Hi,

Thank you for your feedback regarding our doc product and the issue you encountered when deleting rows from a document table.
We have tested your reported issue and found that there is some logic problem in your code. Specifically, the variable table.Rows.Count is affected by the deletion of rows, causing issues with your for loop as the count changes dynamically.
To avoid this issue, please see the following code and screenshot for reference.
Code: Select all
            for (int i = table.Rows.Count - 1; i >= 0; i--)
            {
                Console.WriteLine(table.Rows[i].ToString());
                bool shouldDelete = true;

                TableCell firstCell = table.Rows[i].Cells[0];
                TableCell secondCell = table.Rows[i].Cells[1];

                for (int p = 0; p < firstCell.Paragraphs.Count; p++)
                {
                    if (string.IsNullOrEmpty(firstCell.Paragraphs[p].Text.Trim()) == false)
                    {
                        shouldDelete = false;
                        break;
                    }
                }

                if (shouldDelete)
                {
                    table.Rows.RemoveAt(i);
                }
            }


Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Tue Jul 11, 2023 6:21 am

Hi,

Greetings from E-iceblue.
How is your issue now? Could you please give us some feedback at your convenience? If you have any relevant questions, just feel free to contact us.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.Doc