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:
C# |
Copy Code |
---|---|
//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:
C# |
Copy Code |
---|---|
//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:
C# |
Copy Code |
---|---|
//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:
C# |
Copy Code |
---|---|
//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.