Document Solutions for Word
Features / Table / Row
In This Topic
    Row
    In This Topic

    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.

    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.
    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");
    Back to Top

    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.
    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");
    Back to Top

    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.
    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);
    Back to Top

    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.
    Back to Top
    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");
    Back to Top

    For more information about implementation of rows using DsWord, see DsWord sample browser.