Insert table row

Hi,
I need to copy last row of one table in a document and add that row to another table which is in other document. I used the code like this:

Document mainSec = new Document(oQualItemTemplate123);
Table tab = mainSec.LastSection.Body.Tables[0];
Document testDoc = new Document(oQualItemTemplate);
Table tab1 = testDoc.LastSection.Body.Tables[0];
Row refRow = tab1.LastRow;
Row clone = (Row) refRow.Clone(true);
clone.GetChildNodes(NodeType.Paragraph, true).Clear();
foreach(Cell cell in clone.Cells)
{
    cell.EnsureMinimum();
}
tab.Rows.Add(clone); // getting error here

But I am getting an error like, The newChild was created from a different document than the one that created this node. Please let me know how to resolve this?
Regards,
Srinu Dhulipalla

Hi

Thanks for your request. If you need to insert node from one document into another document, you should first import it. You should use ImportNode method to achieve this.
https://reference.aspose.com/words/net/aspose.words/documentbase/importnode/
Please see the code below:

// Open first docuemtn and get the table
Document doc1 = new Document(@"Test186\in1.doc");
Table tab1 = doc1.LastSection.Body.Tables[0];
// Open the second table and get the table
Document doc2 = new Document(@"Test186\in2.doc");
Table tab2 = doc2.LastSection.Body.Tables[0];
// Import last row of the first table
Node dstRow = doc2.ImportNode(tab1.LastRow, true, ImportFormatMode.KeepSourceFormatting);
// Insert row into the second table
tab2.Rows.Add(dstRow);
// Save the docuemnt
doc2.Save(@"Test186\out.doc");

Hope this helps.
Best regards.