Skip to main content
Row

PivotDataFieldCollection.Add(PivotField, String) Method

Adds a field to the data area of a PivotTable report.

Namespace: DevExpress.Spreadsheet

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

NuGet Package: DevExpress.Spreadsheet.Core

Declaration

PivotDataField Add(
    PivotField field,
    string name
)

Parameters

Name Type Description
field PivotField

A field that should be added to the data area.

name String

A name for the data field. This value is assigned to the PivotDataField.Name property.

Returns

Type Description
PivotDataField

The data field added to the pivot table.

Remarks

The following example demonstrates how to create a simple PivotTable report.

View Example

Dim sourceWorksheet As Worksheet = workbook.Worksheets("Data1")
Dim worksheet As Worksheet = workbook.Worksheets.Add()
workbook.Worksheets.ActiveWorksheet = worksheet

' Create a pivot table.
Dim pivotTable As PivotTable = worksheet.PivotTables.Add(sourceWorksheet("A1:D41"), worksheet("B2"))

' Add the "Product" field to the row axis area.
pivotTable.RowFields.Add(pivotTable.Fields("Product"))
' Add the "Category" field to the column axis area.
pivotTable.ColumnFields.Add(pivotTable.Fields("Category"))
' Add the "Sales" field to the data area and specify the custom field name.
Dim dataField As PivotDataField = pivotTable.DataFields.Add(pivotTable.Fields("Sales"), "Sales(Sum)")
' Specify the number format for the "Sales" field.
dataField.NumberFormat = "_([$$-409]* #,##0.00_);_([$$-409]* (#,##0.00);_([$$-409]* "" - ""??_);_(@_)"
' Add the "Region" field to the filter area.
pivotTable.PageFields.Add(pivotTable.Fields("Region"))

Reorder Data Fields

Use the PivotDataField.MoveDown, PivotDataField.MoveUp, PivotDataField.MoveToBeginning, and PivotDataField.MoveToEnd methods to reorder fields in the data area. The PivotLayout.DataOnRows property allows you to change the orientation of multiple data fields in the report.

Change Summary Function

Use the PivotDataField.SummarizeValuesBy property to change the default summary function used to calculate values in a data field. The PivotDataField.ShowValuesWithCalculation method allows you to apply an additional calculation option to the data field and specify how summary values are displayed within the field.

Remove Data Fields

Use the PivotDataFieldCollection.Remove or PivotDataFieldCollection.RemoveAt method to remove a data field from a pivot table. The PivotDataFieldCollection.Clear method removes all fields from the data area.

The following code snippets (auto-collected from DevExpress Examples) contain references to the Add(PivotField, 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.

See Also