When I am creating a table with rows that have different cell numbers- the rows can't have same width

Hello,

When I am creating a tables that contains dynamic cells in rows, I can’t keep the rows staying in same width. I tried the following methods and all failed.

Method #1:

Table table=new Table(doc);
Row row1 = new Row(doc);
row1.Cells.Add(new Cell(doc));
table.Rows.Add(row1);

Row row2 = new Row(doc);
row2.Cells.Add(new Cell(doc));
row2.Cells.Add(new Cell(doc)); 
table.Rows.Add(row2);

Method #2:
Using DocumentBuilder to build the above table.

Method #3:
Creating a 2X2 table, and then merge the 2 cells in the 1st Row.

Please see attached screen shot. Please let me know how I can create the expected table with rows staying in same width.

Thank you!

Hi Jiang,

Thanks for your inquiry. You can achieve your requirements by using CellFormat.HorizontalMerge property. I suggest you please read following documentation link for your kind reference.
https://docs.aspose.com/words/net/working-with-columns-and-rows/

Following code example creates a table with two rows with cells in the first row horizontally merged.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertCell();
builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.Write("Text in merged cells.");
builder.InsertCell();
// This cell is merged to the previous and should be empty.
builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.EndRow();
builder.InsertCell();
builder.CellFormat.HorizontalMerge = CellMerge.None;
builder.Write("Text in one cell.");
builder.InsertCell();
builder.Write("Text in another cell.");
builder.EndRow();
builder.EndTable();
doc.Save(MyDir + "AsposeOut.docx");

Moreover, please read following forum link for your kind reference.
https://forum.aspose.com/t/76508

Hope this helps you. Please let us know if you have any more queries.

Hello Tahir Manzoor,

So sorry for the delay as I am Chinese in a different time zone.

Thank you so much for your response!

But I tried this solution before I sent this query to you. But I failed. Now I will attached what I got in my document.

I am using Aspose.Words version 11.5.0.0. My IDE is VS 2010. My project is under .net 3.5.

Thank you!

Best Regards,
Jiang.

Hi Jiang,

Thanks for your inquiry. We encourage you to please use the latest version of Aspose.Words as it contains newly introduced features, enhancements and fixes for issues reported earlier. So, I would suggest you please upgrade to the latest version (v13.2.0) from here:
https://releases.aspose.com/words/net

The new version of Aspose.Words will solve your problem. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.