# Table

Learn how to work with tables in a Word document using DsWord. You can even set the styles and indents for a table.

## Content

DsWord allows you to add, modify, and delete a table from a Word document. A table can be created in a document using [Add](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.TableCollection.Add.html) or [Insert](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.TableCollection.Insert.html) method of the [TableCollection](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.TableCollection.html) class. It also lets you format the table using the [TableFormat](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.TableFormat.html) class properties and apply styles to the table using [Style](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.Style.html) class properties.
![Table in a Word document](https://cdn.mescius.io/document-site-files/images/d623c730-bceb-4e85-a6b2-a3dbe84720b2/images/table.png)

## Create Table

To create a table in Word document using DsWord:

1. Create a new Word document using an instance of the **GcWordDocument** class.
2. Get range of the first section in body of the document and add an empty table to the first section using the **Add** method.
3. Add rows and cells to the table using the **Add** method.
4. Create a new table style using the [Style](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.Style.html) class and apply it on the table.
5. Save the document using the [Save](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.GcWordDocument.Save.html) method.

    ```csharp
    GcWordDocument doc = new GcWordDocument();
    //Access the first section of the document
    Section section = doc.Body.Sections.First;
    //Add a paragraph in the section
    Paragraph header = section.GetRange().Paragraphs.Add(" Adding a table:");
    
    // Add an empty table with:
    int rows = 6;
    int cols = 4;
    Table t = section.GetRange().Tables.Add(0, 0);
    
    // Add some rows and cells to it:
    List<string> cells = new List<string>(cols);
    for (int col = 0; col < cols; ++col)
        cells.Add(string.Empty);
    for (int row = 0; row < rows; ++row)
    {
        for (int col = 0; col < cols; ++col)
            cells[col] = $"Row {row + 1}, col {col + 1}";
        t.Rows.Add(cells.ToArray());
    }
    
    // Create a new table style:
    Style ts1 = doc.Styles.Add("Table Style 1", StyleType.Table);
    foreach (var border in ts1.Table.Borders)
    {
        border.LineStyle = LineStyle.BasicBlackDots;
        border.LineWidth = 0.5f;
        border.Color.RGB = Color.Purple;
    }
    
    //Apply the table style on the table
    t.Style = ts1;
    
    //Save the document
    doc.Save("CreateTable.docx");
    ```

## Modify Table

To modify the table formatting in a Word document:

1. Load the document created in [Create Table](https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/features/tables/table#create-table) section, using the [Load](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.GcWordDocument.Load.html) method.
2. Access formatting properties of the table through [Format](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.Table.Format.html) property of the [Table](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.Table.html) class.
3. Modify the table. For example, modify the alignment of the table using the [Alignment](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.TableFormatBase.Alignment.html) property and set the [AllowAutoFit](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.TableFormatBase.AllowAutoFit.html) property to allow the table cells to automatically resize according to their content.

    ```csharp
    //Load an existing document
    doc.Load("CreateTable.docx");
    
    //Access the table
    Table t = doc.Body.Sections.First.GetRange().Tables[0];
    
    //Modify the table
    t.Format.AllowAutoFit = true;
    t.Format.Alignment = TableAlignment.Center;
    
    //Save the document
    doc.Save("ModifiedDoc.docx");
    ```

## Delete Table

To delete a table from a Word document:

1. Access the table from the table collection using [Tables](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.RangeBase.Tables.html) property of the **RangeBase** class.
2. Delete the table using the [Delete](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.ContentObject.Delete.html) method.

    ```csharp
    //Load an existing document
    doc.Load("CreateTable.docx");
    
    //Access the table
    Table t = doc.Body.Sections.First.GetRange().Tables[0];
    
    //Delete the table
    t.Delete();
    
    //Save the document
    doc.Save("TableDeleted.docx");
    ```

## AutoFit Table

To allow automatic resizing of cells in a table according to their content, use **AllowAutoFit** property of the [TableFormatBase](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.TableFormatBase.html) class.

```csharp
//Load an existing document
doc.Load("CreateTable.docx");

//Access the table and allow automatic resizing of the cells
Table t = doc.Body.Sections.First.GetRange().Tables[0];
t.Format.AllowAutoFit = true;

//Save the document
doc.Save("AutoFitEnabled.docx");
```

## Split Table

To spit a table in a Word document:

1. Access a table and then access its row. For example, access the second row of the table.
2. Split the table from the specified row of an existing table.
3. Insert a paragraph between the two tables. This enables you to view two separate tables.

    ```csharp
    doc.Load("CreateTable.docx");
    
    //Access the table and it's second row
    Table t = doc.Body.Sections.First.GetRange().Tables[0];
    Row row = t.Rows[1];
    
    //This generates a new table from the specified row of an existing table.
    Table splitTable1 = t.Split(row, InsertLocation.After);
    
    //Insert a paragraph (without content) between the two tables
    splitTable1.GetRange().Paragraphs.Insert(InsertLocation.Before);
    
    //Save the document
    doc.Save("SplitTable.docx");
    ```

## Set Table Styles

To set table styles in a Word document:

1. Access the table from the table collection using **Tables** property of the **RangeBase** class.
2. Add a new table style to the style collection using [Add](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.StyleCollection.Add.html) method of [StyleCollection](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.StyleCollection.html) class.
3. Apply the style on the table using [Style](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.Table.Style.html) property of **Table** class.
4. Set formatting properties of the table, such as Description, Alignment, and Title and save the document.

    ```csharp
    //Load the document
    doc.Load("CreateTable.docx");
    
    //Access the table
    Table t = doc.Body.Sections.First.GetRange().Tables[0];
    
    // Create a new table style:
    Style ts1 = doc.Styles.Add("Table Style 2", StyleType.Table);
    ts1.Table.ColumnStripe=3;
    
    // Assign the style to the table:
    t.Style = ts1;
    
    //Set table formatting
    t.Format.Alignment = TableAlignment.Center;
    t.Format.Title = "Test Table";
    t.Format.Description = "This is the table description";
    
    //Save the document
    doc.Save("SetTableInfo.docx");
    ```

## Get Table Styles

To get table styles and formatting:

1. Access the table from the table collection using **Tables** property of the **RangeBase** class.
2. Fetch the existing table styles using the **Style** property and the table formatting using the **Format** property.
3. Display the fetched details on the console.

    ```csharp
    //Load the document
    doc.Load("SetTableInfo.docx");
    
    //Access the table
    Table t = doc.Body.Sections.First.GetRange().Tables[0];
    
    //Get table styles
    Style st=t.Style;
    uint cstripe = st.Table.ColumnStripe;   
    Console.WriteLine("Table stripe: " + cstripe.ToString());
    
    //Get table formatting
    TableFormat tformat = t.Format;
    string title = tformat.Title;
    
    //Write the fetched title of the table on the console
    Console.WriteLine("Table Title: " + title);
    ```

## Set Indents

To set the indentation of a table in Word document:

1. Add a new table style to the style collection using the **Add** method.
2. Set the indentation of the table, in points, using the [Indent](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.TableFormatBase.Indent.html) property
3. Apply the style on the table using the **Style** property.

    ```csharp
    //Load the table
    doc.Load("CreateTable.docx");
    
    //Access the table and allow automatic resizing of the cells
    Table t = doc.Body.Sections.First.GetRange().Tables[0];
    t.Format.AllowAutoFit = true;
    
    // Create a new table style to set the Indentation
    Style ts1 = doc.Styles.Add("Table Style 2", StyleType.Table);
    
    //Set the indentation
    ts1.Table.Indent = 3;
    
    //Apply the style
    t.Style = ts1;
    
    //Save the document
    doc.Save("SetIndent.docx");
    ```

## Get Indents

To get the indentation of a table from a Word document:

1. Access the table from the table collection using **Tables** property of the **RangeBase** class.
2. Fetch indentation of the table using **Indent** property.
3. Display the fetched table indentation on the console.

    ```csharp
    //Load an existing document
    doc.Load("SetIndent.docx");
    
    //Access the table
    Table t = doc.Body.Sections.First.GetRange().Tables[0];
    
    //Get the style applied on the table
    Style s = t.Style;
    
    //Get the indentation
    float indenting=s.Table.Indent;  
    
    //Write the fetched indentation(in points) on the console
    Console.WriteLine("Get Indenting: " + indenting);
    ```

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