Skip to main content
All docs
V25.1
  • PivotGridFieldCollection.AddDataSourceColumn(String, PivotArea, PivotGroupInterval) Method

    Adds a field to the PivotGridFieldCollection collection.

    Namespace: DevExpress.XtraPivotGrid

    Assembly: DevExpress.XtraPivotGrid.v25.1.dll

    NuGet Package: DevExpress.Win.PivotGrid

    Declaration

    public PivotGridField AddDataSourceColumn(
        string columnName,
        PivotArea area,
        PivotGroupInterval groupInterval
    )

    Parameters

    Name Type Description
    columnName String

    A String value that specifies a column name.

    area PivotArea

    A PivotArea value that identifies the area in which the new PivotGridField object will be positioned.

    groupInterval PivotGroupInterval

    A PivotGroupInterval enumeration that specifies how the values are combined into groups.’

    Returns

    Type Description
    PivotGridField

    The Pivot Grid field added to the collection.

    Remarks

    AddDataSourceColumn creates a Pivot Grid field, populates it with data, and adds the field to PivotGridFieldCollection. The AddDataSourceColumn method creates the DataSourceColumnBinding object for a Pivot Grid field by ColumnName in OLAP, Server, and Optimized modes. The Pivot Grid field obtains its values from the column in the data source.

    You can specify the following field settings:

    To create Pivot Grid fields for all columns in the bound data source, call PivotGridControl.RetrieveFields.

    Use the PivotGridControl.Fields property to access PivotGridFieldCollection. You can add or remove fields in the collection. For instance, you can add a calculated field to the collection that contains arbitrary data in the Pivot Grid control. Refer to the following topic for more information about calculated fields: Bind Pivot Grid Fields to Calculated Expressions.

    The following code snippet shows how to create fields and populate the Pivot Grid with data:

    using DevExpress.XtraPivotGrid;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace testWinApp {
        public partial class Form1 : Form {
            public Form1() {
                InitializeComponent();
                // This line of code is generated by Data Source Configuration Wizard
                // Fill the SqlDataSource
                sqlDataSource1.Fill();
                pivotGridControl1.OptionsData.DataProcessingEngine = PivotDataProcessingEngine.Optimized;
    
                FillPivot(pivotGridControl1);
                pivotGridControl1.BestFitRowArea();
                pivotGridControl1.CollapseAllRows();
            }
            public static void FillPivot(PivotGridControl pivot) {
                pivot.Fields.AddDataSourceColumn("Country", PivotArea.ColumnArea);
                pivot.Fields.AddDataSourceColumn("ProductName", PivotArea.RowArea).AreaIndex = 1;
                pivot.Fields.AddDataSourceColumn("CategoryName", PivotArea.RowArea, PivotGroupInterval.Alphabetical).AreaIndex = 0;
                pivot.Fields.AddDataSourceColumn("ExtendedPrice", PivotArea.DataArea);
            }
        }
    }
    

    The image below illustrates the result.

    Pivot Grid populated with data

    See Also