Articles in this section
Category / Section

Is it possible to add/remove column from the table ?

1 min read

 

Yes, it is possible add or delete column from a table. There is no property to directly add or remove columns from the table. By Microsoft Word design rows in a table in a document are completely independent. It means each row can have any number of cells of any width. So if you need to add column from MS Word table you have to add cell for each row in table. So we have to add cell for each row by looping. Please refer the below code snippet to achieve this...>

C#

//To add/remove column into the table

for (int i = 0; i < table.Rows.Count ; i++)

{

WTableCell cell = new WTableCell(document);

cell.AddParagraph().AppendText(i.ToString());

table.Rows[i].Cells.Add(cell);

//or Use Addcell method to add new cell

//table.Rows[i].AddCell().AddParagraph().AppendText(i.ToString());

//Insert colum at specific column

//table.Rows[i].Cells.Insert(table.Rows[i].Cells.Count-1, cell);

//Set width of the cell

table.Rows[i].Cells[2].Width = 50;

table.Rows[i].Cells[3].Width = 50;

//To remove cell from the row

table.Rows[i].Cells.RemoveAt(0);

}

VB

For i As Integer = 0 To table.Rows.Count - 1

''To add/remove column into the table

Dim cell As New WTableCell(document)

cell.AddParagraph().AppendText(i.ToString())

table.Rows(i).Cells.Add(cell)

''or Use Addcell method to add new cell

''table.Rows[i].AddCell().AddParagraph().AppendText(i.ToString());

''Insert colum at specific column

''table.Rows[i].Cells.Insert(table.Rows[i].Cells.Count-1, cell);

''Set width of the cell

table.Rows(i).Cells(2).Width = 50

table.Rows(i).Cells(3).Width = 50

''To remove cell from the row

table.Rows(i).Cells.RemoveAt(0)

Next

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied