Skip to main content
A newer version of this page is available. .

PivotGridControl.RetrieveFields(PivotArea, Boolean) Method

Creates PivotGridField objects for all the fields in the bound data source, and moves the fields to the specified area, making them visible or hidden.

Namespace: DevExpress.XtraPivotGrid

Assembly: DevExpress.XtraPivotGrid.v20.1.dll

NuGet Package: DevExpress.Win.PivotGrid

Declaration

public void RetrieveFields(
    PivotArea area,
    bool visible
)

Parameters

Name Type Description
area PivotArea

A PivotArea value that specifies the area to which the created fields are moved.

visible Boolean

true if the created fields are made visible; otherwise, false.

Remarks

This overload clears the field collection and adds new PivotGridField objects to the collection for all the fields in the control’s bound data source. The PivotGridFieldBase.FieldName property of each field created is set to the respective bound field’s name. The created fields are moved to the area specified by the area parameter. The visible parameter specifies whether the fields are made visible or hidden.

Use the PivotGridControl.Fields property to access the field collection. It allows fields to be manually added and removed. For instance, you can add an unbound field to the collection which will represent arbitrary data within the PivotGrid control. For more information on unbound fields, refer to the PivotGridFieldBase.UnboundType topic.

To create fields asynchronously, use the PivotGridControl.RetrieveFieldsAsync method.

To learn more about fields, see Pivot Grid Fields.

Example

If you have a cube on the OLAP server (Microsoft Analysis Services), you can view its data using the Pivot Grid. This example demonstrates how to specify connection settings and create fields that represent measures and dimensions of the cube.

To bind the Pivot Grid control to an OLAP cube, follow the steps below.

  • Set ADOMD as data provider using the PivotGridControl.OLAPDataProvider property.
  • Specify connection settings using the PivotGridControl.OLAPConnectionString property. The following connection string is used in this example:

    Provider=MSOLAP;Data Source=http://demos.devexpress.com/Services/OLAP/msmdpump.dll;Initial catalog=Adventure Works DW Standard Edition;Cube name=Adventure Works;Query Timeout=100;

  • Create fields for measures and dimension in the bound OLAP cube. Use the PivotGridControl.RetrieveFields method overload that create fields, moves them to the specified area and makes them hidden. Another option is creating a new field and specifying its PivotGridFieldBase.OLAPExpression property.
  • Place the fields to the Pivot Grid Control areas as required and make them visible by setting the PivotGridFieldBase.Visible property.

Use the invoked Customization Form to arrange fields.

Note

The complete sample project How to connect a Pivot Grid to an OLAP datasource is available at the DevExpress Examples repository on GitHub.

using DevExpress.XtraPivotGrid;
using DevExpress.XtraPivotGrid.Customization;

namespace WinOlapRetrieveFieldsExample
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
            // Specify the OLAP connection settings.
            pivotGridControl1.OLAPDataProvider = OLAPDataProvider.Adomd;
            pivotGridControl1.OLAPConnectionString =
                @"Provider=MSOLAP;
                Data Source=http://demos.devexpress.com/Services/OLAP/msmdpump.dll; 
                Initial catalog=Adventure Works DW Standard Edition;
                Cube name=Adventure Works;
                Query Timeout=100;";

            // Set the Customization Forms style.
            pivotGridControl1.OptionsCustomization.CustomizationFormStyle = CustomizationFormStyle.Excel2007;
            // Invoke the Customization Form.
            pivotGridControl1.FieldsCustomization();
        }

        private void btnRetrieveFields_Click(object sender, System.EventArgs e)
        {
            // Retrieve fields.
            pivotGridControl1.RetrieveFields(PivotArea.ColumnArea, false);

            // Add fields from the Field List to the specified area to create a report.
            pivotGridControl1.BeginUpdate();
            pivotGridControl1.Fields["[Customer].[Country].[Country]"].Area = PivotArea.RowArea;
            pivotGridControl1.Fields["[Customer].[Country].[Country]"].OLAPDimensionCaption = "TEST";
            pivotGridControl1.Fields["[Customer].[Country].[Country]"].Visible = true;
            pivotGridControl1.Fields["[Customer].[City].[City]"].Area = PivotArea.RowArea;
            pivotGridControl1.Fields["[Customer].[City].[City]"].Visible = true;
            pivotGridControl1.Fields["[Date].[Fiscal].[Fiscal Year]"].Area = PivotArea.ColumnArea;
            pivotGridControl1.Fields["[Date].[Fiscal].[Fiscal Year]"].Visible = true;
            pivotGridControl1.Fields["[Measures].[Internet Sales Amount]"].Visible = true;
            pivotGridControl1.EndUpdate();

            // Resize columns automatically.
            pivotGridControl1.BestFit();

            // Invoke the Customization Form.
            pivotGridControl1.FieldsCustomization();
        }

        private void btnCreateFields_Click(object sender, System.EventArgs e)
        {
            pivotGridControl1.BeginUpdate();
            pivotGridControl1.Fields.Clear();

            // Create a field, specify a query expression to obtain data and a caption to display it in the Customization form.
            PivotGridFieldBase fieldCountry = pivotGridControl1.Fields.Add("Country", PivotArea.RowArea);
            fieldCountry.OLAPExpression = "[Customer].[Country].[Country]";
            fieldCountry.OLAPDimensionCaption = "Location";

            PivotGridFieldBase fieldCity = pivotGridControl1.Fields.Add("City", PivotArea.RowArea);
            fieldCity.OLAPExpression = "[Customer].[City].[City]";
            fieldCity.OLAPDimensionCaption = "Location";

            PivotGridField measureField = new PivotGridField() { Caption = "Cleared Amount", Area = PivotArea.DataArea };
            measureField.OLAPExpression = "[Measures].[Internet Sales Amount] * 0.87";
            measureField.OLAPDimensionCaption = "Sales";
            pivotGridControl1.Fields.Add(measureField);

            PivotGridFieldBase fieldTop10 = pivotGridControl1.Fields.Add("Top10", PivotArea.ColumnArea);
            fieldTop10.OLAPExpression = "TOPCOUNT([Date].[Date].[Date].MEMBERS, 10, [Measures].[Internet Sales Amount])";
            fieldTop10.OLAPDimensionCaption = "Top";
            fieldTop10.Visible = false;;

            pivotGridControl1.EndUpdate();

            // Invoke the Customization Form.
            pivotGridControl1.FieldsCustomization();
        }
    }
}

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