[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Collapse]Office File API
  Demo Application
  Redistribution and Deployment
 [Collapse]Spreadsheet Document API
   Product Structure
   Getting Started
  [Expand]Spreadsheet Document
   Supported Formats
  [Expand]Cell Basics
  [Expand]Spreadsheet Formulas
   Defined Names
   Data Binding
  [Expand]Pivot Tables
  [Expand]Shapes, Pictures, Charts
  [Expand]Mail Merge
  [Collapse]Examples
   [Expand]Files
   [Expand]Workbooks
   [Expand]Worksheets
   [Expand]Rows and Columns
   [Expand]Cells
   [Expand]Formulas
   [Expand]Import and Export Data
   [Expand]Data Binding
    Sorting
   [Expand]Mail Merge
    Search
   [Expand]Shapes
   [Expand]Charts
   [Expand]Sparklines
   [Expand]Formatting Cells
   [Expand]Conditional Formatting
   [Expand]Grouping
   [Expand]Filtering
   [Collapse]Tables
     How to: Create a Table
     How to: Perform Calculations in a Table
     How to: Apply a Table Style
     How to: Create, Modify And Delete Table Styles
   [Expand]Pivot Tables
   [Expand]Printing
   [Expand]Pictures
   [Expand]Protection
 [Expand]Word Processing Document API
 [Expand]PDF Document API
 [Expand]Excel Export Library
 [Expand]Snap Report API
 [Expand]Zip Compression and Archive API
 [Expand]Barcode Generation API
 [Expand]Unit Conversion API
 [Expand]API Reference
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

How to: Perform Calculations in a Table

The example below demonstrates how to perform calculations on data in a table using column names. The initial table 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.

  1. Access table columns by their indexes in the table column collection (TableColumnCollection), returned by the Table.Columns property.
  2. Add a new column using the TableColumnCollection.Add method, and 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"
  3. 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 names.
  4. Set the Table.ShowTotals property to true to display the total row at the bottom of the table.
  5. Specify the function to calculate the total amount. To do this, set the TableColumn.TotalRowFunction property of the "Amount" column to TotalRowFunction.Sum.

    Tip

    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.

  6. 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.

The image below illustrates the result (the workbook is opened in Microsoft® Excel®).

Expanded See Also

Is this topic helpful?​​​​​​​