Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Build an Application
 [Collapse]Controls and Libraries
  [Expand]Forms and User Controls
  [Expand]Messages, Notifications and Dialogs
  [Expand]Editors and Simple Controls
  [Expand]Ribbon, Bars and Menu
  [Expand]Application UI Manager
  [Expand]Docking Library
  [Expand]Data Grid
  [Expand]Vertical Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Chart Control
  [Expand]Diagrams
  [Expand]Gauges
  [Expand]Map Control
  [Expand]Scheduler
  [Collapse]Spreadsheet
   [Expand]Product Information
    Product Structure
    Getting Started
   [Expand]Spreadsheet Document
    Supported Formats
   [Expand]Cell Basics
   [Expand]Spreadsheet Formulas
    Defined Names
    Data Binding
    Data Validation
   [Expand]Data Presentation
    Data Grouping
   [Expand]Pivot Table Overview
   [Expand]Charting Overview
    Printing
    Events
   [Expand]Mail Merge Overview
    Protection
    Find and Replace
   [Expand]Visual Elements
    Services
   [Expand]Shortcuts
   [Collapse]Examples
    [Expand]Files
    [Expand]Workbooks
    [Expand]Worksheets
    [Expand]Rows and Columns
    [Expand]Cells
    [Expand]Formulas
    [Expand]Import and Export Data
    [Expand]Data Binding
    [Expand]Mail Merge
    [Expand]Formatting Cells
    [Expand]Conditional Formatting
    [Expand]Group Data
    [Expand]Filter Data
    [Collapse]Tables
      How to: Create a Table
      How to: Apply a Table Style to a Table
      How to: Create, Modify and Delete Table Styles
      How to: Set Default Table Style
    [Expand]Pivot Tables
    [Expand]Printing
    [Expand]Pictures
    [Expand]Charts
    [Expand]Protection
    [Expand]Customization
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]Printing-Exporting
  [Expand]PDF Viewer
   Reporting
  [Expand]Snap
  [Expand]TreeMap Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

How to: Create a Table

Show Me

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E4832.

This example demonstrates how to create a table from a range of cells. The sample range includes a list of products and invoice information on each product: price, quantity and discount.

The resulting table will provide an additional column to calculate the amount per product, and an additional row to show the total amount. Follow the steps below:

  1. Create a Table

    Add a new Table object to the worksheet's collection of tables (Worksheet.Tables) via the TableCollection.Add method. Pass the following parameters:

    • The range of cells that you wish to format as a table. Use the "B2:F5" range reference instead of "B2:E5" to include an additional column to the right that calculates and displays product amounts.
    • A Boolean value indicating that the top row of the specified range will be the table header.
  2. Format the Table

    Format the table by applying one of the built-in table styles. To do this, set the Table.Style property to the table style object from the IWorkbook.TableStyles collection. Access the desired style by its BuiltInTableStyleId identifier.

  3. Table Columns

    Access table columns by their indexes from the column collection that the Table.Columns property returns.
  4. Table Header Row

    To specify the last column header, set its TableColumn.Name property to "Amount". Headers of other table columns are automatically set to the values of the corresponding cells:
    • table.Columns[0].Name = "Product"
    • table.Columns[1].Name = "Price"
    • table.Columns[2].Name = "Quantity"
    • table.Columns[3].Name = "Discount"
  5. Calculated Column

    Specify the formula to calculate the product amount, and assign it to the Amount column using the TableColumn.Formula property. In the formula, refer to table columns by their headers.

  6. Table Total Row

    Set the Table.ShowTotals property to true, to display the total row at the bottom of the table.

  7. Total Row Function

    Specify the function to calculate the total amount. To do this, set the TableColumn.TotalRowFunction property of the Amount column to TotalRowFunction.Sum.

    Note

    In the total row, you can use any formulas you wish, not only functions listed by the TotalRowFunction enumerator. To use a custom formula in the total row, assign it to the TableColumn.TotalRowFormula property of the required table column.

  8. Table Ranges and Data Ranges

    Specify number formats to display numbers as currency values in the Price and Amount columns, and as percentage values in the Discount column. To access the data range of a table column, use the TableColumn.DataRange property.

    Use the Table.HeaderRowRange and Table.TotalRowRange properties to access table header and total row ranges, and set the alignment.

    Change the width of table columns. To do this, access the table range via the Table.Range property, and use its Range.ColumnWidthInCharacters property.

  9. The image below shows the results.

How would you rate this topic?​​​​​​​