PivotGridField Class
A field within the PivotGridControl.
Namespace: DevExpress.XtraPivotGrid
Assembly: DevExpress.XtraPivotGrid.v24.2.dll
Declaration
public class PivotGridField :
PivotGridFieldBase,
IPivotGridViewInfoDataOwner,
ISupportLookAndFeel,
ISupportDXSkinColorsEx,
ISupportDXSkinColors,
IAccessiblePropertiesProvider
Related API Members
The following members return PivotGridField objects:
Remarks
A PivotGridField object is a field in the Pivot Grid. Fields are visualized with field headers. You can drag and drop a field between Pivot Grid areas to modify the Pivot Grid layout.
Create and Position Pivot Grid Fields
To visualize data in the Pivot Grid, create Pivot Grid fields and position them in the header areas.
A field can be positioned within one of four areas: Column, Row, Data, and Filter Header Area. The field’s function depends on the area in which it is located:
- Column Header Area
- Lists field values along the control’s top edge. Field values are column headers.
- Row Header Area
- Lists field values along the control’s left edge. Field values are row headers.
- Data Header Area
- Calculates summaries against the fields in column and row areas. The summaries are calculated for all cells, and each cell is identified by a column and a row.
- Filter Header Area
- Displays headers of filter fields. Filter fields allow users to filter the entire Pivot Grid to display data for the predefined values from the filter dropdown list.
Refer to the following article for more information: Field Layout.
Follow the steps below to create and position fields in code:
- Create a
PivotGridField
object and add it to the PivotGridControl.Fields collection. - Specify the field’s area and position within this area. For this, use the PivotGridFieldBase.Area and PivotGridFieldBase.AreaIndex properties.
AreaIndex
can be set only after the field is added to the control’s field collection.
Bind Pivot Grid Fields to Data
Pivot Grid uses the Binding API to bind Pivot Grid fields to data. Data binding sources can be columns in a data source, calculated expressions, or window calculations.
Refer to the following help topics for more information on how to bind Pivot Grid fields to different data binding sources:
- Bind Pivot Grid Fields to Data Columns
- Bind Pivot Grid Fields to Calculated Expressions
- Bind Pivot Grid Fields to Window Calculations
Tip
To obtain distinct field values which can be used to filter a Pivot Grid’s data, call the PivotGridFieldBase.GetUniqueValues method. See the Filtering Overview topic for more information on Pivot Grid’s data filtering.
Obtain Field Data
Use the following methods to obtain a field’s data:
- PivotGridFieldBase.GetUniqueValues
- Returns an array of the unique values which are stored in the underlying data source in the current field.
- PivotGridFieldBase.GetAvailableValues
- Returns a list of field values that are not excluded by filters applied to other fields and can be displayed.
- PivotGridFieldBase.GetVisibleValues
- Returns the field values currently shown in the PivotGrid.
Example
This example demonstrates how to create the Pivot Grid fields in code and specify their location and format. The Pivot Grid’s data source is the ExcelDataSource instance, created in code.
using DevExpress.DataAccess.Excel;
using DevExpress.XtraEditors;
using DevExpress.XtraPivotGrid;
using System;
namespace WinFormsPivotGridDataFieldsExample {
public partial class Form1 : XtraForm {
public Form1() {
InitializeComponent();
this.Load += Form1_Load;
// Create the Excel Data Source.
ExcelDataSource ds = new ExcelDataSource();
ds.FileName = "SalesPerson.xlsx";
ExcelWorksheetSettings settings = new ExcelWorksheetSettings("Data");
ds.SourceOptions = new ExcelSourceOptions(settings);
ds.Fill();
// Set the pivot's data source.
pivotGridControl1.DataSource = ds;
// Create pivot grid fields.
PivotGridField fieldCategoryName = new PivotGridField() {
Area = PivotArea.RowArea,
AreaIndex = 0,
Caption = "Category Name"
};
// Bind fields to columns in the data source.
DataSourceColumnBinding categoryNameBinding = new DataSourceColumnBinding("CategoryName");
fieldCategoryName.DataBinding = categoryNameBinding;
PivotGridField fieldProductName = new PivotGridField() {
Area = PivotArea.RowArea,
AreaIndex = 1,
Caption = "Product Name"
};
DataSourceColumnBinding productNameBinding = new DataSourceColumnBinding("ProductName");
fieldProductName.DataBinding = productNameBinding;
PivotGridField fieldExtendedPrice = new PivotGridField() {
Area = PivotArea.DataArea,
AreaIndex = 0,
Caption = "Extended Price"
};
DataSourceColumnBinding extendedPriceBinding = new DataSourceColumnBinding("Extended Price");
fieldExtendedPrice.DataBinding = extendedPriceBinding;
// Specify the field format.
fieldExtendedPrice.CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
fieldExtendedPrice.CellFormat.FormatString = "c2";
PivotGridField fieldOrderDate1 = new PivotGridField() {
Area = PivotArea.ColumnArea,
AreaIndex = 0,
Caption = "Year"
};
DataSourceColumnBinding fieldOrderDate1Binding = new DataSourceColumnBinding("OrderDate");
fieldOrderDate1Binding.GroupInterval = PivotGroupInterval.DateYear;
fieldOrderDate1.DataBinding = fieldOrderDate1Binding;
PivotGridField fieldOrderDate2 = new PivotGridField() {
Area = PivotArea.ColumnArea,
AreaIndex = 1,
Caption = "Quarter"
};
DataSourceColumnBinding fieldOrderDate2Binding = new DataSourceColumnBinding("OrderDate");
fieldOrderDate2Binding.GroupInterval = PivotGroupInterval.DateQuarter;
fieldOrderDate2.DataBinding = fieldOrderDate2Binding;
PivotGridField fieldCountry = new PivotGridField() {
AreaIndex = 0,
Caption = "Country"
};
DataSourceColumnBinding countryBinding = new DataSourceColumnBinding("Country");
fieldCountry.DataBinding = countryBinding;
// Create a field's filter.
fieldCountry.FilterValues.Clear();
fieldCountry.FilterValues.FilterType = PivotFilterType.Included;
fieldCountry.FilterValues.Add("USA");
// Add fields to the pivot grid.
pivotGridControl1.Fields.AddRange(new PivotGridField[] {
fieldCategoryName,
fieldProductName,
fieldOrderDate1,
fieldOrderDate2,
fieldExtendedPrice,
fieldCountry});
}
private void Form1_Load(object sender, EventArgs e) {
pivotGridControl1.BestFit();
}
}
}