Back to Devexpress

PivotCalculatedItemCollection.Add(String, String) Method

officefileapi-devexpress-dot-spreadsheet-dot-pivotcalculateditemcollection-dot-add-x28-system-dot-string-system-dot-string-x29.md

latest7.5 KB
Original Source

PivotCalculatedItemCollection.Add(String, String) Method

Creates a new calculated item and appends it to the collection.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
PivotItem Add(
    string formula,
    string name
)
vb
Function Add(
    formula As String,
    name As String
) As PivotItem

Parameters

NameTypeDescription
formulaString

A String value that specifies the formula for the item.

| | name | String |

A String value that specifies the name of the item.

|

Returns

TypeDescription
PivotItem

A PivotItem object that specifies the newly created calculated item.

|

Remarks

Use the Add method to add a calculated item to a PivotTable field. A calculated item uses a formula to calculate its value based on values of other items in the same field where the calculated item resides.

The following code demonstrates how to create two calculated items to calculate total sales for each region.

View Example

csharp
Worksheet worksheet = workbook.Worksheets["Report10"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Access the pivot table by its name in the collection.
PivotTable pivotTable = worksheet.PivotTables["PivotTable1"];

// Access the pivot field by its name in the collection.
PivotField field = pivotTable.Fields["State"];

// Add calculated items to the "State" field.
field.CalculatedItems.Add("=Arizona+California+Colorado", "West Total");
field.CalculatedItems.Add("=Illinois+Kansas+Wisconsin", "Midwest Total");
vb
Dim worksheet As Worksheet = workbook.Worksheets("Report10")
workbook.Worksheets.ActiveWorksheet = worksheet

' Access the pivot table by its name in the collection.
Dim pivotTable As PivotTable = worksheet.PivotTables("PivotTable1")

' Access the pivot field by its name in the collection.
Dim field As PivotField = pivotTable.Fields("State")

' Add calculated items to the "State" field.
field.CalculatedItems.Add("=Arizona+California+Colorado", "West Total")
field.CalculatedItems.Add("=Illinois+Kansas+Wisconsin", "Midwest Total")

For details on how to insert and modify calculated items, refer to the How to: Create a Calculated Item example.

The following code snippets (auto-collected from DevExpress Examples) contain references to the Add(String, String) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-spreadsheet-pivot-table-api-examples/CS/SpreadsheetWPFPivotTableExamples/CodeExamples/PivotCalculatedItemActions.cs#L20

csharp
// Add calculated items to the "State" field.
field.CalculatedItems.Add("=Arizona+California+Colorado", "West Total");
field.CalculatedItems.Add("=Illinois+Kansas+Wisconsin", "Midwest Total");

winforms-spreadsheet-pivot-table-api/CS/SpreadsheetPivotTableExamples/CodeExamples/PivotCalculatedItemActions.cs#L20

csharp
// Add calculated items to the "State" field.
field.CalculatedItems.Add("=Arizona+California+Colorado", "West Total");
field.CalculatedItems.Add("=Illinois+Kansas+Wisconsin", "Midwest Total");

spreadsheet-document-api-pivot-table-examples/CS/SpreadsheetDocServerPivotAPI/CodeExamples/PivotCalculatedItemActions.cs#L26

csharp
// Add calculated items to the "State" field.
field.CalculatedItems.Add("=Arizona+California+Colorado", "West Total");
field.CalculatedItems.Add("=Illinois+Kansas+Wisconsin", "Midwest Total");

wpf-spreadsheet-pivot-table-api-examples/VB/SpreadsheetWPFPivotTableExamples/CodeExamples/PivotCalculatedItemActions.vb#L17

vb
' Add calculated items to the "State" field.
field.CalculatedItems.Add("=Arizona+California+Colorado", "West Total")
field.CalculatedItems.Add("=Illinois+Kansas+Wisconsin", "Midwest Total")

winforms-spreadsheet-pivot-table-api/VB/SpreadsheetPivotTableExamples/CodeExamples/PivotCalculatedItemActions.vb#L17

vb
' Add calculated items to the "State" field.
field.CalculatedItems.Add("=Arizona+California+Colorado", "West Total")
field.CalculatedItems.Add("=Illinois+Kansas+Wisconsin", "Midwest Total")

spreadsheet-document-api-pivot-table-examples/VB/SpreadsheetDocServerPivotAPI/CodeExamples/PivotCalculatedItemActions.vb#L23

vb
' Add calculated items to the "State" field.
field.CalculatedItems.Add("=Arizona+California+Colorado", "West Total")
field.CalculatedItems.Add("=Illinois+Kansas+Wisconsin", "Midwest Total")

See Also

How to: Create a Calculated Item

PivotCalculatedItemCollection Interface

PivotCalculatedItemCollection Members

DevExpress.Spreadsheet Namespace