[]
DsWord provides the Row class representing a table row element. A row can be added to a table through Add method of the RowCollection class. DsWord provides you the access to the formatting properties of table row using Format property of the Row class along with the RowFormat class properties. In addition to formatting a row, DsWord allows you to get the row index, which can be done using Index property of the Row class.
To add a new row to a table:
Access the table from the table collection using Tables property of the RangeBase class.
Add a row to the table using Add method of the RowCollection class.
//Load the document and access the table
doc.Load("CreateTable.docx");
Table t=doc.Body.Sections.First.GetRange().Tables[0];
//Add a new row
string[] newrow = new string[4] { "NR1", "NR2", "NR3", "NR4" };
t.Rows.Add(newrow);
//Save the document
doc.Save("RowAdded.docx");
To delete a row from a table:
Access the table from the table collection using Tables property of the RangeBase class.
Delete a row of the table using the Delete method.
//Load the document and access the table
doc.Load("CreateTable.docx");
Table t = doc.Body.Sections.First.GetRange().Tables[0];
//Delete the fourth row from the table
t.Rows[3].Delete();
//Save the document
doc.Save("RowDeleted.docx");
To fetch the row index from a table:
Access the table from the table collection using Tables property of the RangeBase class.
Get the row index of third row in the table using the Index property.
Display the row index on the console.
//Load the document and access the table
doc.Load("CreateTable.docx");
Table t = doc.Body.Sections.First.GetRange().Tables[0];
//Get the row index
int i=t.Rows[2].Index;
//write the row index on the console
Console.WriteLine("Row Index for selected row is:" + i);
To format the rows in a table:
Access the table from the table collection using Tables property of the RangeBase class.
Set alignment for the table row using Alignment property of the RowFormat class.
Set spacing between the cells using Spacing property of the RowFormat class.
//Load the document and access the table
doc.Load("CreateTable.docx");
Table t = doc.Body.Sections.First.GetRange().Tables[0];
//Set the alignment of a row and spacing between the cells
t.Rows[0].Format.Alignment = TableAlignment.Right;
t.Rows[0].Format.Spacing = 4F;
//Save the document
doc.Save("FormattedRow.docx");
For more information about implementation of rows using DsWord, see DsWord sample browser.