Skip to main content

Binding to Data Overview

  • 4 minutes to read

The ASPxPivotGrid control should be connected to an external data source that provides data you wish to display and process. Like other data-bound web server controls, ASPxPivotGrid can be bound to any standard data source type, including SqlDataSource, ObjectDataSource, XmlDataSource and AccessDataSource. As an alternative, you can use specific DevExpress components to bind the Pivot Grid to data (such as the EntityServerModeDataSource or LinqServerModeDataSource components that can be used to perform data processing server side).

Bind a Pivot Grid to Data at Design Time

To bind an ASPxPivotGrid to a data source at design time, click its smart tag and select one of the following options.

ProvidingData_ChooseDatasource

  • The Choose Data Source combo box allows you to select the existing project data source.

    If necessary, you can create a new data source using the New Data Source… command.

  • Choose OLAP Data Source allows you to connect to a cube in a Microsoft Analysis Services server. To learn more, see OLAP Data Source.

After connecting the pivot grid to a data source, create the required fields. To do this, invoke the Fields and Groups page and click the Retrieve Fields button (the ASPxPivotGrid Designer - Retrieve Fields Button icon).

ASPxPivotGrid Designer - Fields and Groups Page

Automatically created fields are located in the Filter Header Area by default. To rearrange fields and locate them in the required areas, use the PivotGridFieldBase.Area property.

To learn more about pivot grid fields, see Fields.

Bind a Pivot Grid to Data in Code

To bind ASPxPivotGrid to a data source at runtime, assign the data source (for example, an AccessDataSource instance) to the ASPxPivotGrid.DataSource property.

You can create pivot grid fields automatically for all data source fields using the ASPxPivotGrid.RetrieveFields method, or populate the ASPxPivotGrid.Fields collection manually with PivotGridField instances bound to specific data source fields.

To learn more about how to create and arrange pivot grid fields, see Fields.

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. Assign the AccessDataSource instance to the ASPxPivotGrid.DataSource property to bind the Pivot Grid to the created data source. Then call the ASPxPivotGrid.RetrieveFields method to generate DataSourceColumnBinding objects for each Pivot Grid field.

View Example

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
    Inherits="ASPxPivotGrid_RuntimeDataBinding._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server"/>
</body>
</html>
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxPivotGrid;
using DevExpress.XtraPivotGrid;
using System.Linq;

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

            // Initializes a data source.
            ds = new SqlDataSource("System.Data.OleDb","Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\nwind.mdb",
                "SELECT [CategoryName], [ProductName], [ProductSales], [ShippedDate] FROM [ProductReports]");

            // Initializes ASPxPivotGrid.
            ASPxPivotGrid1 = new ASPxPivotGrid();
            ASPxPivotGrid1.OptionsData.DataProcessingEngine = PivotDataProcessingEngine.Optimized;

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

            // Places the Pivot Grid onto a page.
            form1.Controls.Add(ASPxPivotGrid1);

            if (ASPxPivotGrid1.Fields.Count != 0) 
                return;

            // Creates Pivot Grid fields for all data source columns.
            ASPxPivotGrid1.RetrieveFields();

            // 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"].DataBinding as DataSourceColumnBinding).GroupInterval = PivotGroupInterval.DateYear;

        }
    }
}