Back to Devexpress

PivotDataFieldCollection.Add(PivotField) Method

officefileapi-devexpress-dot-spreadsheet-dot-pivotdatafieldcollection-dot-add-x28-devexpress-dot-spreadsheet-dot-pivotfield-x29.md

latest8.4 KB
Original Source

PivotDataFieldCollection.Add(PivotField) Method

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

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
PivotDataField Add(
    PivotField field
)
vb
Function Add(
    field As PivotField
) As PivotDataField

Parameters

NameTypeDescription
fieldPivotField

A field that should be added to the data area.

|

Returns

TypeDescription
PivotDataField

The data field added to the pivot table.

|

Remarks

The following example creates a simple PivotTable report:

View Example

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

' Create a pivot table using the cell range "A1:D41" as the data source.
Dim pivotTable As PivotTable = worksheet.PivotTables.Add(sourceWorksheet("A1:D41"), worksheet("B2"))

' Add the "Category" field to the row axis area.
pivotTable.RowFields.Add(pivotTable.Fields("Category"))
' Add the "Product" field to the row axis area.
pivotTable.RowFields.Add(pivotTable.Fields("Product"))
' Add the "Sales" field to the data area.
pivotTable.DataFields.Add(pivotTable.Fields("Sales"))
csharp
Worksheet sourceWorksheet = workbook.Worksheets["Data1"];
Worksheet worksheet = workbook.Worksheets.Add();
workbook.Worksheets.ActiveWorksheet = worksheet;

// Create a pivot table using the cell range "A1:D41" as the data source.
PivotTable pivotTable = worksheet.PivotTables.Add(sourceWorksheet["A1:D41"], worksheet["B2"]);

// Add the "Category" field to the row axis area.
pivotTable.RowFields.Add(pivotTable.Fields["Category"]);
// Add the "Product" field to the row axis area.
pivotTable.RowFields.Add(pivotTable.Fields["Product"]);
// Add the "Sales" field to the data area.
pivotTable.DataFields.Add(pivotTable.Fields["Sales"]);

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) 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/PivotTableActions.cs#L23

csharp
// Add the "Sales" field to the data area.
pivotTable.DataFields.Add(pivotTable.Fields["Sales"]);

winforms-spreadsheet-pivot-table-api/CS/SpreadsheetPivotTableExamples/CodeExamples/PivotTableActions.cs#L23

csharp
// Add the "Sales" field to the data area.
pivotTable.DataFields.Add(pivotTable.Fields["Sales"]);
#endregion #CreateFromRange

spreadsheet-document-api-pivot-table-examples/CS/SpreadsheetDocServerPivotAPI/CodeExamples/ValueFieldSettingsActions.cs#L32

csharp
// Add the "Amount" field to the data area.
PivotDataField dataField = pivotTable.DataFields.Add(pivotTable.Fields["Amount"]);
// Use the "Average" function to summarize values in the data field.

wpf-spreadsheet-pivot-table-api-examples/VB/SpreadsheetWPFPivotTableExamples/CodeExamples/ValueFieldSettingsActions.vb#L20

vb
' Add the "Amount" field to the data area.
Dim dataField As PivotDataField = pivotTable.DataFields.Add(pivotTable.Fields("Amount"))
' Use the "Average" function to summarize values in the data field.

winforms-spreadsheet-pivot-table-api/VB/SpreadsheetPivotTableExamples/CodeExamples/ValueFieldSettingsActions.vb#L20

vb
' Add the "Amount" field to the data area.
Dim dataField As PivotDataField = pivotTable.DataFields.Add(pivotTable.Fields("Amount"))
' Use the "Average" function to summarize values in the data field.

spreadsheet-document-api-pivot-table-examples/VB/SpreadsheetDocServerPivotAPI/CodeExamples/ValueFieldSettingsActions.vb#L29

vb
' Add the "Amount" field to the data area.
Dim dataField As PivotDataField = pivotTable.DataFields.Add(pivotTable.Fields("Amount"))
' Use the "Average" function to summarize values in the data field.

See Also

Pivot Tables: Examples

PivotDataFieldCollection Interface

PivotDataFieldCollection Members

DevExpress.Spreadsheet Namespace