Back to Devexpress

PivotField.Formula Property

officefileapi-devexpress-dot-spreadsheet-dot-pivotfield-19772807.md

latest7.1 KB
Original Source

PivotField.Formula Property

Gets or sets the formula for the calculated field.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
string Formula { get; set; }
vb
Property Formula As String

Property Value

TypeDescription
String

A String value that is the calculated field’s formula.

|

Remarks

Use the Formula property to modify the formula of the existing calculated field. To add a calculated field to a pivot table, use the PivotCalculatedFieldCollection.Add method of the PivotTable.CalculatedFields collection.

View Example

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

// Access the pivot table by its name in the collection.
PivotTable pivotTable = worksheet.PivotTables["PivotTable1"];
// Create a calculated field based on data in the "Sales" field.
pivotTable.CalculatedFields.Add("=Sales*10%", "Sales Tax Rate 10");
// Access the calculated field by its name in the collection.
PivotField field = pivotTable.CalculatedFields["Sales Tax Rate 10"];
//Change the formula for the calculated field.
field.Formula = "=Sales*15%";
//Change the calculated field name.
field.Name = "Sales Tax Rate 15";
//Add the calculated field to the data area and specify the custom field name.
PivotDataField dataField = pivotTable.DataFields.Add(field, "Total Tax");
// Specify the number format for the data field.
dataField.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* (#,##0.00);_([$$-409]* "" - ""??_);_(@_)";
vb
Dim worksheet As Worksheet = workbook.Worksheets("Report1")
workbook.Worksheets.ActiveWorksheet = worksheet

' Access the pivot table by its name in the collection.
Dim pivotTable As PivotTable = worksheet.PivotTables("PivotTable1")
' Create a calculated field based on data in the "Sales" field.
pivotTable.CalculatedFields.Add("=Sales*10%", "Sales Tax Rate 10")
' Access the calculated field by its name in the collection.
Dim field As PivotField = pivotTable.CalculatedFields("Sales Tax Rate 10")
'Change the formula for the calculated field.
field.Formula = "=Sales*15%"
'Change the calculated field name.
field.Name = "Sales Tax Rate 15"
'Add the calculated field to the data area and specify the custom field name.
Dim dataField As PivotDataField = pivotTable.DataFields.Add(field, "Total Tax")
' Specify the number format for the data field.
dataField.NumberFormat = "_([$$-409]* #,##0.00_);_([$$-409]* (#,##0.00);_([$$-409]* "" - ""??_);_(@_)"

For the pivot fields that originate from the source data, the Formula property returns an empty string.

The following code snippets (auto-collected from DevExpress Examples) contain references to the Formula property.

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/PivotCalculatedFieldActions.cs#L56

csharp
//Change the formula for the calculated field.
field.Formula = "=Sales*15%";
//Change the calculated field name.

winforms-spreadsheet-pivot-table-api/CS/SpreadsheetPivotTableExamples/CodeExamples/PivotCalculatedFieldActions.cs#L56

csharp
//Change the formula for the calculated field.
field.Formula = "=Sales*15%";
//Change the calculated field name.

spreadsheet-document-api-pivot-table-examples/CS/SpreadsheetDocServerPivotAPI/CodeExamples/PivotCalculatedFieldActions.cs#L62

csharp
//Change the formula for the calculated field.
field.Formula = "=Sales*15%";
//Change the calculated field name.

wpf-spreadsheet-pivot-table-api-examples/VB/SpreadsheetWPFPivotTableExamples/CodeExamples/PivotCalculatedFieldActions.vb#L51

vb
'Change the formula for the calculated field.
field.Formula = "=Sales*15%"
'Change the calculated field name.

winforms-spreadsheet-pivot-table-api/VB/SpreadsheetPivotTableExamples/CodeExamples/PivotCalculatedFieldActions.vb#L51

vb
'Change the formula for the calculated field.
field.Formula = "=Sales*15%"
'Change the calculated field name.

spreadsheet-document-api-pivot-table-examples/VB/SpreadsheetDocServerPivotAPI/CodeExamples/PivotCalculatedFieldActions.vb#L57

vb
'Change the formula for the calculated field.
field.Formula = "=Sales*15%"
'Change the calculated field name.

See Also

CalculatedFields

Add

PivotField Interface

PivotField Members

DevExpress.Spreadsheet Namespace