Skip to main content

PivotGridFieldCollection.AddDataSourceColumn(String, PivotArea) Method

Adds a field to the PivotGridFieldCollection collection.

Namespace: DevExpress.XtraPivotGrid

Assembly: DevExpress.XtraPivotGrid.v24.2.dll

Declaration

public PivotGridField AddDataSourceColumn(
    string columnName,
    PivotArea area
)

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.

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 specify GroupInterval, call the AddDataSourceColumn(String, PivotArea, PivotGroupInterval) method overload.

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

Use the PivotGridControl.Fields property to access PivotGridFieldCollection. For instance, you can add a calculated field to a 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

The following code snippets (auto-collected from DevExpress Examples) contain references to the AddDataSourceColumn(String, PivotArea) 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.

See Also