CellCollection

CellCollection class

Provides typed access to a collection of Cell nodes.

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

public class CellCollection : NodeCollection

Properties

NameDescription
Count { get; }Gets the number of nodes in the collection.
Item { get; }Retrieves a Cell at the given index. (2 indexers)

Methods

NameDescription
Add(Node)Adds a node to the end of the collection.
Clear()Removes all nodes from this collection and from the document.
Contains(Node)Determines whether a node is in the collection.
GetEnumerator()Provides a simple “foreach” style iteration over the collection of nodes.
IndexOf(Node)Returns the zero-based index of the specified node.
Insert(int, Node)Inserts a node into the collection at the specified index.
Remove(Node)Removes the node from the collection and from the document.
RemoveAt(int)Removes the node at the specified index from the collection and from the document.
ToArray()Copies all cells from the collection to a new array of cells. (2 methods)

Examples

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");
}

See Also