windowsforms-devexpress-dot-xtrapivotgrid-dot-pivotgridfieldcollection-dot-adddatasourcecolumn-x28-system-dot-string-devexpress-dot-xtrapivotgrid-dot-pivotarea-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
)
Public Function AddDataSourceColumn(
columnName As String,
area As PivotArea
) 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.
|
| 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 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);
}
}
}
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.
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.
winforms-pivot-grid-hide-specific-columns-and-rows/CS/Data.cs#L21
public static void FillPivot(PivotGridControl pivot) {
pivot.Fields.AddDataSourceColumn(Employee, PivotArea.RowArea);
pivot.Fields.AddDataSourceColumn(Widget, PivotArea.RowArea);
winforms-pivot-grid-hide-empty-columns-and-rows/CS/Data.cs#L21
public static void FillPivot(PivotGridControl pivot) {
pivot.Fields.AddDataSourceColumn(Employee, PivotArea.RowArea);
pivot.Fields.AddDataSourceColumn(Widget, PivotArea.RowArea);
winforms-pivot-split-field-value-cells/CS/Data.cs#L21
public static void FillPivot(PivotGridControl pivot) {
pivot.Fields.AddDataSourceColumn(Employee, PivotArea.RowArea);
pivot.Fields.AddDataSourceColumn(Widget, PivotArea.RowArea);
private void InitializeFields() {
pivotGridControl1.Fields.AddDataSourceColumn("Type", PivotArea.RowArea);
pivotGridControl1.Fields.AddDataSourceColumn("Product", PivotArea.RowArea);
winforms-pivot-change-summarydisplaytype-in-context-menu/CS/WindowsApplication34/Form1.cs#L21
pivotGridControl1.Fields.AddDataSourceColumn("Type", DevExpress.XtraPivotGrid.PivotArea.RowArea);
pivotGridControl1.Fields.AddDataSourceColumn("Product", DevExpress.XtraPivotGrid.PivotArea.RowArea);
winforms-pivot-grid-hide-specific-columns-and-rows/VB/Data.vb#L25
Public Shared Sub FillPivot(ByVal pivot As PivotGridControl)
pivot.Fields.AddDataSourceColumn(Employee, PivotArea.RowArea)
pivot.Fields.AddDataSourceColumn(Widget, PivotArea.RowArea)
winforms-pivot-grid-hide-empty-columns-and-rows/VB/Data.vb#L33
Public Sub FillPivot(ByVal pivot As PivotGridControl)
pivot.Fields.AddDataSourceColumn(Employee, PivotArea.RowArea)
pivot.Fields.AddDataSourceColumn(Widget, PivotArea.RowArea)
Private Sub InitializeFields()
pivotGridControl1.Fields.AddDataSourceColumn("Type", PivotArea.RowArea)
pivotGridControl1.Fields.AddDataSourceColumn("Product", PivotArea.RowArea)
winforms-pivot-change-summarydisplaytype-in-context-menu/VB/WindowsApplication34/Form1.vb#L32
Dim dataField As PivotGridField = pivotGridControl1.Fields.AddDataSourceColumn("Number", DevExpress.XtraPivotGrid.PivotArea.DataArea)
dataField.Caption = "Number"
See Also
PivotGridFieldCollection Class