# Cell

Learn how to add a new cell in a table, merge two cells vertically or horizontally, and apply formatting to cells using DsWord. Learn more in DsWord docs.

## Content

In DsWord, a cell element is represented by the [Cell](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.Cell.html) class which allows you to access each cell of a table which can be added through [Add](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.CellCollection.Add.html) method of the [CellCollection](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.CellCollection.html) class.
Each element of a table, such as row, column, and cell, can have different styles and formatting. DsWord allows you to set the table cell formatting using [Format](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.Cell.Format.html) property of the **Cell** class along with the [CellFormat](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.CellFormat.html) class properties. It also lets you merge cells, both vertically and horizontally, in a table. The [VerticalMerge](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.CellFormat.VerticalMerge.html) property allows a cell to merge vertically with the other cells in a table. Similarly, the [HorizontalMerge](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.CellFormat.HorizontalMerge.html) property allows a cell to merge horizontally with the other cells in a table.

## Set Cell Formatting

To set cell formatting in a table:

1. Access the table from the table collection using **Tables** property of the **RangeBase** class.
2. Set cell formatting. For example, set flow direction of the text and padding of third cell in the first row.

    ```csharp
    //Load the document and access the table
    doc.Load("CreateTable.docx");
    Table t = doc.Body.Sections.First.GetRange().Tables[0];
    
    //Set cell formatting
    t.Rows[0].Cells[2].Format.TextFlowDirection = TextFlowDirection.TopToBottomRightToLeft;
    t.Rows[0].Cells[2].Format.Padding.Top = t.Rows[0].Cells[2].Format.Padding.Bottom = 2;
    
    //Save the document
    doc.Save("SetCellFormatting.docx");
    ```

## Get Cell Formatting

To get formatting of a cell from a table in an existing document:

1. Access the table from the table collection using **Tables** property of the **RangeBase** class.
2. Fetch the existing cell formatting using the **Format** property of the **Cell** class. For example, fetch the padding of third cell in the first row.

    ```csharp
    //Load the document and access the table
    doc.Load("SetCellFormatting.docx");
    Table t = doc.Body.Sections.First.GetRange().Tables[0];
    
    //Get cell padding of the third cell in the first row
    CellFormat cformat = t.Rows[0].Cells[2].Format;
    float paddingTop =  cformat.Padding.Top;
    
    //Write the fetched padding on the console
    Console.WriteLine("Top Padding: " + paddingTop);
    ```

## Merge Cells

To merge cells horizontally in a table:

1. Access the table from the table collection using **Tables** property of the **RangeBase** class.
2. Set the second cell of the second row to visually span two following cells, to merge the cells horizontally.
3. Remove the two following cells.

    ```csharp
    //Load the document and access the table
    doc.Load("CreateTable.docx");
    Table t = doc.Body.Sections.First.GetRange().Tables[0];
    
    // Set up cell (1,1) to visually span two following cells, hence merge the cells
    t.Rows[1].Cells[1].Format.GridSpan = 3;
    
    // Remove the 2 following cells (unless we want to create a jagged table)
    t.Rows[1].Cells[3].Delete();
    t.Rows[1].Cells[2].Delete();
    
    //Save the document
    doc.Save("MergedCells.docx");
    ```

## Insert Graphic Elements

To insert a graphic element, such as an image, in a table cell:

1. Access a table in the document.
2. Load picture data into a byte array.
3. Access the cell where you want to add a picture and add the picture into the cell using **Add** method of the [PictureCollection](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.PictureCollection.html) class. For example, access the last cell.
4. Set the picture size and set the **AllowAutoFit** property to true to allow automatic resizing of the cell.

    ```csharp
    doc.Load("CreateTable.docx");
    
    //Access the table
    Table t = doc.Body.Sections.First.GetRange().Tables[0];
    
    // Load picture data
    var picBytes = File.ReadAllBytes(Path.Combine("Resources", "Images", "road.jpg"));
    
    // Add picture, specifying its mime type:
    var pic = t.Rows.Last.Cells.Last.GetRange().Runs.First.GetRange().Pictures.Add(
        picBytes, "image/jpeg");
    
    // Picture size
    pic.Size.Width.Value = 200;
    pic.Size.Height.Value = 100;
    
    //Allow automatic resizing of the cell
    t.Format.AllowAutoFit = true;
    
    //Save the document
    doc.Save("InsertGraphicElement.docx");
    ```

For more information about implementation of cells using DsWord, see [DsWord sample browser](https://developer.mescius.com/documents-api-word/demos/basics/tables/grid-span/code-cs).