Row

Row class

Represents a table row.

To learn more, visit the Working with Tables documentation article.

public class Row : CompositeNode

Constructors

NameDescription
Row(DocumentBase)Initializes a new instance of the Row class.

Properties

NameDescription
Cells { get; }Provides typed access to the Cell child nodes of the row.
Count { get; }Gets the number of immediate children of this node.
CustomNodeId { get; set; }Specifies custom node identifier.
virtual Document { get; }Gets the document to which this node belongs.
FirstCell { get; }Returns the first Cell in the row.
FirstChild { get; }Gets the first child of the node.
HasChildNodes { get; }Returns true if this node has any child nodes.
override IsComposite { get; }Returns true as this node can have child nodes.
IsFirstRow { get; }True if this is the first row in a table; false otherwise.
IsLastRow { get; }True if this is the last row in a table; false otherwise.
LastCell { get; }Returns the last Cell in the row.
LastChild { get; }Gets the last child of the node.
NextRow { get; }Gets the next Row node.
NextSibling { get; }Gets the node immediately following this node.
override NodeType { get; }Returns Row.
ParentNode { get; }Gets the immediate parent of this node.
ParentTable { get; }Returns the immediate parent table of the row.
PreviousRow { get; }Gets the previous Row node.
PreviousSibling { get; }Gets the node immediately preceding this node.
Range { get; }Returns a Range object that represents the portion of a document that is contained in this node.
RowFormat { get; }Provides access to the formatting properties of the row.

Methods

NameDescription
override Accept(DocumentVisitor)Accepts a visitor.
override AcceptEnd(DocumentVisitor)Accepts a visitor for visiting the end of the row.
override AcceptStart(DocumentVisitor)Accepts a visitor for visiting the start of the row.
AppendChild<T>(T)Adds the specified node to the end of the list of child nodes for this node.
Clone(bool)Creates a duplicate of the node.
CreateNavigator()Creates navigator which can be used to traverse and read nodes.
EnsureMinimum()If the Row has no cells, creates and appends one Cell.
GetAncestor(NodeType)Gets the first ancestor of the specified NodeType.
GetAncestor(Type)Gets the first ancestor of the specified object type.
GetChild(NodeType, int, bool)Returns an Nth child node that matches the specified type.
GetChildNodes(NodeType, bool)Returns a live collection of child nodes that match the specified type.
GetEnumerator()Provides support for the for each style iteration over the child nodes of this node.
override GetText()Gets the text of all cells in this row including the end of row character.
IndexOf(Node)Returns the index of the specified child node in the child node array.
InsertAfter<T>(T, Node)Inserts the specified node immediately after the specified reference node.
InsertBefore<T>(T, Node)Inserts the specified node immediately before the specified reference node.
NextPreOrder(Node)Gets next node according to the pre-order tree traversal algorithm.
PrependChild<T>(T)Adds the specified node to the beginning of the list of child nodes for this node.
PreviousPreOrder(Node)Gets the previous node according to the pre-order tree traversal algorithm.
Remove()Removes itself from the parent.
RemoveAllChildren()Removes all the child nodes of the current node.
RemoveChild<T>(T)Removes the specified child node.
RemoveSmartTags()Removes all SmartTag descendant nodes of the current node.
SelectNodes(string)Selects a list of nodes matching the XPath expression.
SelectSingleNode(string)Selects the first Node that matches the XPath expression.
ToString(SaveFormat)Exports the content of the node into a string in the specified format.
ToString(SaveOptions)Exports the content of the node into a string using the specified save options.

Remarks

Row can only be a child of a Table.

Row can contain one or more Cell nodes.

A minimal valid row needs to have at least one Cell.

Examples

Shows how to create a table.

Document doc = new Document();
Table table = new Table(doc);
doc.FirstSection.Body.AppendChild(table);

// Tables contain rows, which contain cells, which may have paragraphs
// with typical elements such as runs, shapes, and even other tables.
// Calling the "EnsureMinimum" method on a table will ensure that
// the table has at least one row, cell, and paragraph.
Row firstRow = new Row(doc);
table.AppendChild(firstRow);

Cell firstCell = new Cell(doc);
firstRow.AppendChild(firstCell);

Paragraph paragraph = new Paragraph(doc);
firstCell.AppendChild(paragraph);

// Add text to the first cell in the first row of the table.
Run run = new Run(doc, "Hello world!");
paragraph.AppendChild(run);

doc.Save(ArtifactsDir + "Table.CreateTable.docx");

Shows how to iterate through all tables in the document and print the contents of each cell.

Document doc = new Document(MyDir + "Tables.docx");
TableCollection tables = doc.FirstSection.Body.Tables;

Assert.AreEqual(2, tables.ToArray().Length);

for (int i = 0; i < tables.Count; i++)
{
    Console.WriteLine($"Start of Table {i}");

    RowCollection rows = tables[i].Rows;

    // We can use the "ToArray" method on a row collection to clone it into an array.
    Assert.AreEqual(rows, rows.ToArray());
    Assert.AreNotSame(rows, rows.ToArray());

    for (int j = 0; j < rows.Count; j++)
    {
        Console.WriteLine($"\tStart of Row {j}");

        CellCollection cells = rows[j].Cells;

        // We can use the "ToArray" method on a cell collection to clone it into an array.
        Assert.AreEqual(cells, cells.ToArray());
        Assert.AreNotSame(cells, cells.ToArray());

        for (int k = 0; k < cells.Count; k++)
        {
            string cellText = cells[k].ToString(SaveFormat.Text).Trim();
            Console.WriteLine($"\t\tContents of Cell:{k} = \"{cellText}\"");
        }

        Console.WriteLine($"\tEnd of Row {j}");
    }

    Console.WriteLine($"End of Table {i}\n");
}

Shows how to build a nested table without using a document builder.

public void CreateNestedTable()
{
    Document doc = new Document();

    // Create the outer table with three rows and four columns, and then add it to the document.
    Table outerTable = CreateTable(doc, 3, 4, "Outer Table");
    doc.FirstSection.Body.AppendChild(outerTable);

    // Create another table with two rows and two columns and then insert it into the first table's first cell.
    Table innerTable = CreateTable(doc, 2, 2, "Inner Table");
    outerTable.FirstRow.FirstCell.AppendChild(innerTable);

    doc.Save(ArtifactsDir + "Table.CreateNestedTable.docx");
}

/// <summary>
/// Creates a new table in the document with the given dimensions and text in each cell.
/// </summary>
private static Table CreateTable(Document doc, int rowCount, int cellCount, string cellText)
{
    Table table = new Table(doc);

    for (int rowId = 1; rowId <= rowCount; rowId++)
    {
        Row row = new Row(doc);
        table.AppendChild(row);

        for (int cellId = 1; cellId <= cellCount; cellId++)
        {
            Cell cell = new Cell(doc);
            cell.AppendChild(new Paragraph(doc));
            cell.FirstParagraph.AppendChild(new Run(doc, cellText));

            row.AppendChild(cell);
        }
    }

    // You can use the "Title" and "Description" properties to add a title and description respectively to your table.
    // The table must have at least one row before we can use these properties.
    // These properties are meaningful for ISO / IEC 29500 compliant .docx documents (see the OoxmlCompliance class).
    // If we save the document to pre-ISO/IEC 29500 formats, Microsoft Word ignores these properties.
    table.Title = "Aspose table title";
    table.Description = "Aspose table description";

    return table;
}

See Also