Delete a row based on a value

Hi Team,

I have generated a Data Table. I also Have many tables generated. After Merging I want to get access to every table and check if any of the row(Any cell in the row) has the keyword “Delete”. If it is there I need to delete the entire row. Please help me on this.

  1. Search for every table
  2. Take rows
  3. Check in any of the cell if “Delete” keyword is there
  4. If yes, Delete the entire row.

If you are about to give me the IFieldMergeCallBack methodology, Please share a sample code which does that.

Thanks,
Prasanna

@pshanmuganathan concern this topic,too . Thanks

@pshanmuganathan, @hzjianglf,

You can add the following code at the end just before Save method call:

Document doc = new Document("E:\\Temp\\in.docx");

ArrayList list = new ArrayList();
foreach(Table tab in doc.GetChildNodes(NodeType.Table, true))
{
    foreach(Row row in tab.Rows)
    {
        foreach(Cell cell in row.Cells)
        {
            if (cell.ToString(SaveFormat.Text).Contains("Delete"))
            {
                list.Add(cell);
            }
        }
    }
}

foreach(Cell cell in list)
{
    cell.ParentRow.Remove();
}

doc.Save("E:\\Temp\\19.4.docx"); 

Hope, this helps.

1 Like

@awais.hafeez thks! perfect!

Hi Awais,

Thanks for the reply. Working Perfectly fine!

However, I have another question here. Can you tell me how to implement the same but delete column instead ?

How to Delete a Entire Column based on a value in the Cell?

Thanks,
Prasanna

@pshanmuganathan,

I believe, you can meet this requirement after reading the following article:
Working with Columns and Rows

In case the problem still remains, please ZIP and upload your input Word document and expected Word document showing the final result here for testing. You can create expected document by using MS Word. We will then investigate the scenario on our end and provide you more information.