---
title: "Work with Columns and Rows in C#"
---


{{% alert color="grey" %}}

## Purpose Summary

This page explains how to work with table columns and rows programmatically.

{{% /alert %}}

For more control over how tables work, learn how to manipulate columns and rows.

## Find the Table Element Index {#find-the-index-of-table-elements}

Columns, rows and cells are managed by accessing the selected document node by its index. Finding the index of any node involves gathering all child nodes of the element type from the parent node, and then using the [IndexOf](https://reference.aspose.com/words/net/aspose.words/nodecollection/indexof/) method to find the index of the desired node in the collection.

### Find the Index of a Table in a Document {#find-the-index-of-table-in-a-document}

Sometimes you may need to make changes to a particular table in a document. To do this, you can refer to a table by its index.

The following code example shows how to retrieve the index of a table in a document:

{{< gist "aspose-words-gists" "7e7e54ead8b97457543ea46fc6bae045" "retrieve-table-index.cs" >}}

### Find the Index of a Row in a Table {#find-the-index-of-a-row-in-a-table}

Similarly, you may need to make changes to a specific row in a selected table. To do this, you can also refer to a row by its index.

The following code example shows how to retrieve the index of a row in a table:

{{< gist "aspose-words-gists" "7e7e54ead8b97457543ea46fc6bae045" "retrieve-row-index.cs" >}}

### Find the Index of a Cell in a Row {#find-the-index-of-a-cell-in-a-row}

Finally, you may need to make changes to a specific cell, and you can do this by cell index as well.

The following code example shows how to retrieve the index of a cell in a row:

{{< gist "aspose-words-gists" "7e7e54ead8b97457543ea46fc6bae045" "retrieve-cell-index.cs" >}}

## Work with Columns {#work-with-columns}

In the Aspose.Words Document Object Model (DOM), the [Table](https://reference.aspose.com/words/net/aspose.words.tables/table/) node consists of [Row](https://reference.aspose.com/words/net/aspose.words.tables/row/) nodes and then [Cell](https://reference.aspose.com/words/net/aspose.words.tables/cell/) nodes. Thus, in the `Document` Object Model of Aspose.Words, as in Word documents, there is no concept of a column.

By design, the table rows in Microsoft Word and Aspose.Words are completely independent, and the basic properties and operations are contained only in the rows and cells of the table. This gives tables the ability to have some interesting attributes:

- Each table row can have a completely different number of cells
- Vertically, the cells of each row can have different widths
- It is possible to join tables with different row formats and number of cells

Any operations performed on columns are actually "shortcuts" that perform the operation by collectively changing row cells in such a way that it looks like they are being applied to columns. That is, you can perform operations on columns by simply iterating over the same table row cell index.

The following code example simplifies such operations by proving a facade class that collects the cells that make up a "column" of a table:

{{< gist "aspose-words-gists" "7e7e54ead8b97457543ea46fc6bae045" "column-class.cs" >}}

The following code example shows how to insert a blank column into a table:

{{< gist "aspose-words-gists" "7e7e54ead8b97457543ea46fc6bae045" "insert-blank-column.cs" >}}

The following code example shows how to remove a column from a table in a document:

{{< gist "aspose-words-gists" "7e7e54ead8b97457543ea46fc6bae045" "remove-column.cs" >}}

## Specify Rows as Header Rows

You can choose to repeat the first row in the table as the Header Row only on the first page or on each page if the table is split into several. In Aspose.Words, you can repeat the Header Row on every page using the [HeadingFormat](https://reference.aspose.com/words/net/aspose.words.tables/rowformat/headingformat/) property.

You can also mark multiple header rows if such rows are located one after the other at the beginning of the table. To do this, you need to apply the **HeadingFormat** properties to these rows.

{{% alert color="primary" %}}

Note that Header Rows do not work in nested tables. That is, if you have a table inside another table, this setting will have no effect. It is a limitation of Microsoft Word that does not allow this, not Aspose.Words.

{{% /alert %}}

The following code example shows how to build a table which includes Header Rows that repeat on subsequent pages:

{{< gist "aspose-words-gists" "7e7e54ead8b97457543ea46fc6bae045" "repeat-rows-on-subsequent-pages.cs" >}}

## Keep Tables and Rows from Breaking Across Pages {#keep-tables-and-rows-from-breaking-across-pages}

There are times where the contents of a table should not be split across pages. For instance, if a title is above a table, the title and table should always be kept together on the same page to preserve proper appearance.

There are two separate techniques that are useful to achieve this functionality:

- `Allow row break across pages`, which is applied to table rows
- `Keep with next`, which is applied to paragraphs in table cells

By default, the above properties are disabled.

### Keep a Row from Breaking Across Pages {#keep-a-row-from-breaking-across-pages}

This involves restricting content inside the cells of a row from being split across a page. In Microsoft Word, this can found under Table Properties as the option “Allow row to break across pages”. In Aspose.Words this is found under the [RowFormat](https://reference.aspose.com/words/net/aspose.words.tables/rowformat/) object of a [Row](https://reference.aspose.com/words/net/aspose.words.tables/row/) as the property [RowFormat.AllowBreakAcrossPages](https://reference.aspose.com/words/net/aspose.words.tables/row/format/properties/allowbreakacrosspages).

The following code example shows how to disable breaking rows across pages for each row in a table:

{{< gist "aspose-words-gists" "7e7e54ead8b97457543ea46fc6bae045" "row-format-disable-break-across-pages.cs" >}}

{{% alert color="primary" %}}

You can download the sample file of this example from [Aspose.Words GitHub](https://github.com/aspose-words/Aspose.Words-for-.NET/blob/master/Examples/Data/Table%20spanning%20two%20pages.docx).

{{% /alert %}}

### Keep a Table from Breaking Across Pages {#keep-a-table-from-breaking-across-pages}

To stop the table from splitting across pages, we need to specify that we want the content contained within the table to stay together.

To do this, Aspose.Words uses a method, which allows users to select a table and enable the [KeepWithNext](https://reference.aspose.com/words/net/aspose.words/paragraphformat/keepwithnext/) parameter to true for each paragraph within the table cells. The exception is the last paragraph in the table, which should be set to false.

The following code example shows how to set a table to stay together on the same page:

{{< gist "aspose-words-gists" "7e7e54ead8b97457543ea46fc6bae045" "keep-table-together.cs" >}}

{{% alert color="primary" %}}

You can download the sample file of this example from [Aspose.Words GitHub](https://github.com/aspose-words/Aspose.Words-for-.NET/blob/master/Examples/Data/Table%20spanning%20two%20pages.docx).

{{% /alert %}}

## Related APIs


- [Table](https://reference.aspose.com/words/net/aspose.words.tables/table/)
- [Row](https://reference.aspose.com/words/net/aspose.words.tables/row/)
- [Cell](https://reference.aspose.com/words/net/aspose.words.tables/cell/)
- [RowFormat.HeadingFormat](https://reference.aspose.com/words/net/aspose.words.tables/rowformat/headingformat/)
- [RowFormat.AllowBreakAcrossPages](https://reference.aspose.com/words/net/aspose.words.tables/row/format/properties/allowbreakacrosspages/)
- [ParagraphFormat.KeepWithNext](https://reference.aspose.com/words/net/aspose.words/paragraphformat/keepwithnext/)
- [NodeCollection.IndexOf](https://reference.aspose.com/words/net/aspose.words/nodecollection/indexof/)

## FAQ

1. **Q:** How can I obtain the index of a specific table, row, or cell in a document?  
   **A:** Retrieve the collection of the desired node type using `Document.GetChildNodes(NodeType.Table, true)`, `NodeType.Row`, or `NodeType.Cell`. Then call `NodeCollection.IndexOf(node)` to get its zero‑based index. This works for any node that is a child of the document or a parent node.

2. **Q:** What is the recommended way to insert a new blank column into an existing table?  
   **A:** Use a helper class that gathers the cells belonging to the target column (by iterating each row and selecting the cell at the column index). For each row, insert a new `Cell` at that position with `Row.Cells.Insert(index, new Cell(document))`. The helper then updates the table layout automatically.

3. **Q:** How do I delete a particular column from a table?  
   **A:** Iterate through every `Row` in the table and call `Row.Cells.RemoveAt(columnIndex)`. After removal, you may need to call `table.AutoFit(AutoFitBehavior.AutoFitToContents)` to adjust column widths.

4. **Q:** How can I make the first row (or multiple rows) repeat as a header on every page?  
   **A:** Set the `Row.RowFormat.HeadingFormat` property. Use `HeadingFormat.FirstRow` for a single header row or `HeadingFormat.AllRows` for consecutive rows that should act as headers. Example: `row.RowFormat.HeadingFormat = HeadingFormat.FirstRow;`.

5. **Q:** What steps are required to prevent a row or an entire table from breaking across pages?  
   **A:** For rows, set `Row.RowFormat.AllowBreakAcrossPages = false`. For tables, set `ParagraphFormat.KeepWithNext = true` on every paragraph inside the table cells except the last paragraph of the table. This keeps the whole table together on a single page.