Back to Devexpress

PivotPageFieldCollection.Add(PivotField) Method

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

latest8.5 KB
Original Source

PivotPageFieldCollection.Add(PivotField) Method

Adds a field to the report filter area of the pivot table.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

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

Parameters

NameTypeDescription
fieldPivotField

A PivotField object specifying a pivot field that should be added to the page area of the PivotTable report.

|

Returns

TypeDescription
PivotPageField

A PivotPageField object which represents the inserted page field.

|

Remarks

To add a new field to the PivotTable’s report filter area at the specified position, use the PivotPageFieldCollection.Insert method. Note that you can also re-order fields in a page area using the MoveDown , MoveUp , MoveToBeginning or MoveToEnd method of the PivotPageField object whose position you wish to change.

To remove a page field from the collection, use the PivotPageFieldCollection.Remove or PivotPageFieldCollection.RemoveAt method. You can also use the PivotPageFieldCollection.Clear method to remove all fields from the PivotTable’s report filter area

Example

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.
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"))
csharp
Worksheet sourceWorksheet = workbook.Worksheets["Data1"];
Worksheet worksheet = workbook.Worksheets.Add();
workbook.Worksheets.ActiveWorksheet = worksheet;

// Create a pivot table.
PivotTable 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.
PivotDataField dataField = 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"]);

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/PivotFieldActions.cs#L24

csharp
// Add the "Region" field to the filter area.
pivotTable.PageFields.Add(pivotTable.Fields["Region"]);

winforms-spreadsheet-pivot-table-api/CS/SpreadsheetPivotTableExamples/CodeExamples/PivotFieldActions.cs#L26

csharp
// Add the "Region" field to the filter area.
pivotTable.PageFields.Add(pivotTable.Fields["Region"]);
#endregion #AddToAxis

spreadsheet-document-api-pivot-table-examples/CS/SpreadsheetDocServerPivotAPI/CodeExamples/PivotFieldActions.cs#L38

csharp
// Add the "Region" field to the filter area.
pivotTable.PageFields.Add(pivotTable.Fields["Region"]);
#endregion #AddToAxis

wpf-spreadsheet-pivot-table-api-examples/VB/SpreadsheetWPFPivotTableExamples/CodeExamples/PivotFieldActions.vb#L25

vb
' Add the "Region" field to the filter area.
pivotTable.PageFields.Add(pivotTable.Fields("Region"))

winforms-spreadsheet-pivot-table-api/VB/SpreadsheetPivotTableExamples/CodeExamples/PivotFieldActions.vb#L27

vb
' Add the "Region" field to the filter area.
pivotTable.PageFields.Add(pivotTable.Fields("Region"))
' #End Region ' #AddToAxis

spreadsheet-document-api-pivot-table-examples/VB/SpreadsheetDocServerPivotAPI/CodeExamples/PivotFieldActions.vb#L35

vb
' Add the "Region" field to the filter area.
            pivotTable.PageFields.Add(pivotTable.Fields("Region"))
#End Region ' #AddToAxis

See Also

Pivot Tables in Spreadsheet Documents

Pivot Table API

How to: Create a Pivot Table

PivotPageFieldCollection Interface

PivotPageFieldCollection Members

DevExpress.Spreadsheet Namespace