Back to Devexpress

PivotFieldReferenceCollection.Add(PivotField) Method

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

latest8.7 KB
Original Source

PivotFieldReferenceCollection.Add(PivotField) Method

Adds a field to the row or column area of the pivot table.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

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

Parameters

NameTypeDescription
fieldPivotField

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

|

Returns

TypeDescription
PivotFieldReference

A PivotFieldReference object which represents the inserted row or column field.

|

Remarks

Use the Add method to add a new row or column field to the end of the PivotTable.RowFields or PivotTable.ColumnFields collection, respectively. To add a field to the PivotTable’s row axis area or column axis area at the specified position, use the PivotFieldReferenceCollection.Insert method. Afterwards, you can re-order fields in the corresponding area using the MoveDown , MoveUp , MoveToBeginning or MoveToEnd method of the row or column field whose position you wish to change.

To remove a row or column field from the collection, use the PivotFieldReferenceCollection.Remove or PivotFieldReferenceCollection.RemoveAt method. You can also use the PivotFieldReferenceCollection.Clear method to remove all fields from the PivotTable’s row axis area or column axis 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 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"]);

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/PivotTableLayoutActions.cs#L16

csharp
// Add the "Region" field to the column axis area.
pivotTable.ColumnFields.Add(pivotTable.Fields["Region"]);

winforms-spreadsheet-pivot-table-api/CS/SpreadsheetPivotTableExamples/CodeExamples/PivotTableLayoutActions.cs#L16

csharp
// Add the "Region" field to the column axis area.
pivotTable.ColumnFields.Add(pivotTable.Fields["Region"]);

spreadsheet-document-api-pivot-table-examples/CS/SpreadsheetDocServerPivotAPI/CodeExamples/PivotFieldGroupingActions.cs#L25

csharp
// Add the "State" field to the column axis area.
pivotTable.ColumnFields.Add(field);

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

vb
' Add the "Category" field to the row axis area.
pivotTable.RowFields.Add(pivotTable.Fields("Category"))
' Add the "Product" field to the row axis area.

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

vb
' Add the "Category" field to the row axis area.
pivotTable.RowFields.Add(pivotTable.Fields("Category"))
' Add the "Product" field to the row axis area.

spreadsheet-document-api-pivot-table-examples/VB/SpreadsheetDocServerPivotAPI/CodeExamples/PivotFieldGroupingActions.vb#L22

vb
' Add the "State" field to the column axis area.
pivotTable.ColumnFields.Add(field)

See Also

Pivot Tables in Spreadsheet Documents

Pivot Table API

How to: Create a Pivot Table

PivotFieldReferenceCollection Interface

PivotFieldReferenceCollection Members

DevExpress.Spreadsheet Namespace