windowsforms-devexpress-dot-xtrapivotgrid-dot-pivotgridfieldcollection-dot-adddatasourcecolumn-x28-system-dot-string-devexpress-dot-xtrapivotgrid-dot-pivotarea-devexpress-dot-xtrapivotgrid-dot-pivotgroupinterval-x29.md
Adds a field to the PivotGridFieldCollection collection.
Namespace : DevExpress.XtraPivotGrid
Assembly : DevExpress.XtraPivotGrid.v25.2.dll
NuGet Package : DevExpress.Win.PivotGrid
public PivotGridField AddDataSourceColumn(
string columnName,
PivotArea area,
PivotGroupInterval groupInterval
)
Public Function AddDataSourceColumn(
columnName As String,
area As PivotArea,
groupInterval As PivotGroupInterval
) As PivotGridField
| 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.’
|
| Type | Description |
|---|---|
| PivotGridField |
The Pivot Grid field added to the collection.
|
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);
}
}
}
Imports DevExpress.XtraPivotGrid
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Namespace testWinApp
Partial Public Class Form1
Inherits Form
Public Sub New()
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()
End Sub
Public Shared Sub FillPivot(ByVal pivot As PivotGridControl)
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)
End Sub
End Class
End Namespace
The image below illustrates the result.
See Also
PivotGridFieldCollection Class