Back to Devexpress

PivotGridControl.Fields Property

windowsforms-devexpress-dot-xtrapivotgrid-dot-pivotgridcontrol-d98775ec.md

latest15.1 KB
Original Source

PivotGridControl.Fields Property

Provides access to a PivotGrid control’s field collection.

Namespace : DevExpress.XtraPivotGrid

Assembly : DevExpress.XtraPivotGrid.v25.2.dll

NuGet Package : DevExpress.Win.PivotGrid

Declaration

csharp
[Browsable(true)]
[XtraSerializableProperty(XtraSerializationVisibility.Collection, true, true, true, 0, XtraSerializationFlags.DefaultValue)]
[XtraSerializablePropertyId(5)]
[DXCategory("Data")]
public PivotGridFieldCollection Fields { get; }
vb
<Browsable(True)>
<XtraSerializableProperty(XtraSerializationVisibility.Collection, True, True, True, 0, XtraSerializationFlags.DefaultValue)>
<XtraSerializablePropertyId(5)>
<DXCategory("Data")>
Public ReadOnly Property Fields As PivotGridFieldCollection

Property Value

TypeDescription
PivotGridFieldCollection

A PivotGridFieldCollection object which represents a collection of all the fields within a PivotGridControl.

|

Remarks

This property stores all the fields present in the PivotGrid control. It allows you to add and delete fields using the appropriate methods.

Individual fields can be accessed using indexed notation or the bound field name as parameters. If the bound field name is used as an indexer and there are several fields with the same name (for example, the GroupInterval feature is used), the first field found in the collection will be returned.

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.

csharp
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();
        }
    }
}
vb
Imports DevExpress.DataAccess.Excel
Imports DevExpress.XtraEditors
Imports DevExpress.XtraPivotGrid
Imports System

Namespace WinFormsPivotGridDataFieldsExample
    Partial Public Class Form1
        Inherits XtraForm

        Public Sub New()
            InitializeComponent()
            AddHandler Me.Load, AddressOf Form1_Load
            ' Create the Excel Data Source.
            Dim ds As New ExcelDataSource()
            ds.FileName = "SalesPerson.xlsx"
            Dim settings As New ExcelWorksheetSettings("Data")
            ds.SourceOptions = New ExcelSourceOptions(settings)
            ds.Fill()
            ' Set the pivot's data source.
            pivotGridControl1.DataSource = ds
            ' Create pivot grid fields.
            Dim fieldCategoryName As New PivotGridField() With {
                .Area = PivotArea.RowArea,
                .AreaIndex = 0,
                .Caption = "Category Name"
            }
            ' Bind fields to columns in the data source.
            Dim categoryNameBinding As New DataSourceColumnBinding("CategoryName")
            fieldCategoryName.DataBinding = categoryNameBinding

            Dim fieldProductName As New PivotGridField() With {
                .Area = PivotArea.RowArea,
                .AreaIndex = 1,
                .Caption = "Product Name"
            }

            Dim productNameBinding As New DataSourceColumnBinding("ProductName")
            fieldProductName.DataBinding = productNameBinding

            Dim fieldExtendedPrice As New PivotGridField() With {
                .Area = PivotArea.DataArea,
                .AreaIndex = 0,
                .Caption = "Extended Price"
            }

            Dim extendedPriceBinding As New DataSourceColumnBinding("Extended Price")
            fieldExtendedPrice.DataBinding = extendedPriceBinding

            ' Specify the field format.
            fieldExtendedPrice.CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            fieldExtendedPrice.CellFormat.FormatString = "c2"

            Dim fieldOrderDate1 As New PivotGridField() With {
                .Area = PivotArea.ColumnArea,
                .AreaIndex = 0,
                .Caption = "Year"
            }

            Dim fieldOrderDate1Binding As New DataSourceColumnBinding("OrderDate")
            fieldOrderDate1Binding.GroupInterval = PivotGroupInterval.DateYear
            fieldOrderDate1.DataBinding = fieldOrderDate1Binding

            Dim fieldOrderDate2 As New PivotGridField() With {
                .Area = PivotArea.ColumnArea,
                .AreaIndex = 1,
                .Caption = "Quarter"
            }

            Dim fieldOrderDate2Binding As New DataSourceColumnBinding("OrderDate")
            fieldOrderDate2Binding.GroupInterval = PivotGroupInterval.DateQuarter
            fieldOrderDate2.DataBinding = fieldOrderDate2Binding

            Dim fieldCountry As New PivotGridField() With {
                .AreaIndex = 0,
                .Caption = "Country"
            }

            Dim countryBinding As 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})
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
            pivotGridControl1.BestFit()
        End Sub
    End Class
End Namespace

The following code snippets (auto-collected from DevExpress Examples) contain references to the Fields property.

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.

winforms-pivot-grid-hide-specific-columns-and-rows/CS/Data.cs#L28

csharp
pivot.Fields.AddDataSourceColumn(Remains, PivotArea.DataArea);
foreach (PivotGridField field in pivot.Fields) {
    field.AllowedAreas = GetAllowedArea(field.Area);

winforms-pivot-split-field-value-cells/CS/Data.cs#L27

csharp
pivot.Fields.AddDataSourceColumn(Quantity, PivotArea.DataArea);
foreach (PivotGridField field in pivot.Fields) {
    field.AllowedAreas = GetAllowedArea(field.Area);

winforms-pivot-grid-hide-empty-columns-and-rows/CS/Form1.cs#L58

csharp
void pivotGridControl1_FieldValueDisplayText(object sender, PivotFieldDisplayTextEventArgs e) {
    if (object.ReferenceEquals(e.Field, pivotGridControl1.Fields[PivotHelper.Month])) {
        e.DisplayText = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName((int)e.Value);

winforms-pivot-grid-connect-to-an-olap-datasource/CS/WinOlapRetrieveFieldsExample/Form1.cs#L26

csharp
pivotGridControl1.RetrieveFields(PivotArea.ColumnArea, false);
foreach (PivotGridField field in pivotGridControl1.Fields){
    field.Name = "field" + (field.DataBinding as DataSourceColumnBinding).ColumnName;

winforms-create-a-custom-exporter-for-pivotgridcontrol-with-xtrareport/CS/Report_at_Runtime/PivotReportGenerator.cs#L95

csharp
if (pivot.Fields[i].Area == area)
        fields.Add(pivot.Fields[i]);
return fields;

winforms-pivot-grid-hide-specific-columns-and-rows/VB/Data.vb#L32

vb
pivot.Fields.AddDataSourceColumn(Remains, PivotArea.DataArea)
For Each field As PivotGridField In pivot.Fields
    field.AllowedAreas = GetAllowedArea(field.Area)

winforms-pivot-split-field-value-cells/VB/Data.vb#L31

vb
pivot.Fields.Add(Quantity, PivotArea.DataArea)
For Each field As PivotGridField In pivot.Fields
    field.AllowedAreas = GetAllowedArea(field.Area)

winforms-pivot-grid-hide-empty-columns-and-rows/VB/Form1.vb#L67

vb
Private Sub pivotGridControl1_FieldValueDisplayText(ByVal sender As Object, ByVal e As PivotFieldDisplayTextEventArgs)
    If ReferenceEquals(e.Field, pivotGridControl1.Fields(Month)) Then
        e.DisplayText = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(CInt(e.Value))

winforms-pivot-grid-connect-to-an-olap-datasource/VB/WinOlapRetrieveFieldsExample/Form1.vb#L27

vb
pivotGridControl1.RetrieveFields(PivotArea.ColumnArea, False)
For Each field As PivotGridField In pivotGridControl1.Fields
    field.Name = "field" & TryCast(field.DataBinding, DataSourceColumnBinding).ColumnName

winforms-create-a-custom-exporter-for-pivotgridcontrol-with-xtrareport/VB/Report_at_Runtime/PivotReportGenerator.vb#L96

vb
For i As Integer = 0 To pivot.Fields.Count - 1
    If pivot.Fields(CInt((i))).Area = area Then fields.Add(pivot.Fields(i))
Next

See Also

RetrieveFields

PivotGridControl Class

PivotGridControl Members

DevExpress.XtraPivotGrid Namespace