[]
        
(Showing Draft Content)

Row

GcWord provides the Row class representing a table row element. A row can be added to a table through Add method of the RowCollection class. GcWord 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, GcWord allows you to get the row index, which can be done using Index property of the Row class.

Add Row

To add a new row to a table:

  1. Access the table from the table collection using Tables property of the RangeBase class.
  2. 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");

Delete Row

To delete a row from a table:

  1. Access the table from the table collection using Tables property of the RangeBase class.
  2. 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");

Get Row Index

To fetch the row index from a table:

  1. Access the table from the table collection using Tables property of the RangeBase class.
  2. Get the row index of third row in the table using the Index property.
  3. 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);

Format Rows

To format the rows in a table:

  1. Access the table from the table collection using Tables property of the RangeBase class.
  2. Set alignment for the table row using Alignment property of the RowFormat class.
  3. 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 GcWord, see GcWord sample browser.