How to: Create and Bind a Pivot Grid to Data in Code

  • 2 minutes to read

This example demonstrates how to create an ASPxPivotGrid and bind it to data in code.

In this example, the ASPxPivotGrid and System.Web.UI.WebControls.AccessDataSource instances are created and initialized in code. The Pivot Grid is bound to data by assigning the AccessDataSource instance to the ASPxPivotGrid.DataSource property. Then, the ASPxPivotGrid.RetrieveFields method is used to create pivot grid fields for all data source fields.

View Example

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxPivotGrid;
using DevExpress.XtraPivotGrid;

namespace ASPxPivotGrid_RuntimeDataBinding {
    public partial class _Default : Page {
        private AccessDataSource ds;
        private ASPxPivotGrid ASPxPivotGrid1;
        protected override void OnLoad(EventArgs e) {

            // Initializes a data source.
            ds = new AccessDataSource("~/nwind.mdb", "SELECT [CategoryName]," +
                "[ProductName], [ProductSales], [ShippedDate] FROM [ProductReports]");

            // Initializes ASPxPivotGrid.
            ASPxPivotGrid1 = new ASPxPivotGrid();

            // Binds ASPxPivotGrid to the data source.
            ASPxPivotGrid1.DataSource = ds;

            // Places the Pivot Grid onto a page.

            if (ASPxPivotGrid1.Fields.Count != 0) 

            // Creates pivot grid fields for all data source fields.

            // Locates the pivot grid fields in appropriate areas.
            ASPxPivotGrid1.Fields["CategoryName"].Area = PivotArea.RowArea;
            ASPxPivotGrid1.Fields["ProductName"].Area = PivotArea.RowArea;
            ASPxPivotGrid1.Fields["ShippedDate"].Area = PivotArea.ColumnArea;
            ASPxPivotGrid1.Fields["ProductSales"].Area = PivotArea.DataArea;

            ASPxPivotGrid1.Fields["ShippedDate"].GroupInterval = PivotGroupInterval.DateYear;