Using the data from my first post in this series Getting started with Pivot Grid (Shaping Data) I will create a simple ASP.NET application to display that data.
This video will walk you through the steps in this post.
- Create a new ASP.NET Web Site project
- Open the Default.aspx page in Design view
- Add a ASPxPivotGrid control on the the design surface
- In the ASPxPivotGrid Tasks menu or the DataSourceID property window select “<New data source>”
- Select SQL Database
- Connect to an installed instance of the NorthWind database
- Select “Specify a custom SQL statement or stored procedure”
- Use the SQL from the “Shaping Data” blog post
SELECT DISTINCT
Categories.CategoryName,
Products.ProductName,
([Order Details].UnitPrice * [Order Details].Quantity) * (1 - [Order Details].Discount) / 100 * 100 AS ProductSales,
Orders.ShippedDate
FROM Products INNER JOIN
Categories ON Products.CategoryID = Categories.CategoryID INNER JOIN
[Order Details] ON Products.ProductID = [Order Details].ProductID INNER JOIN
Orders ON [Order Details].OrderID = Orders.OrderID
WHERE (Orders.ShippedDate IS NOT NULL)
At this point you have the PivotGrid aware of the data set you created but it dose not know what to show. We need to add PivotGrid fields for each field from the data set we want to display. This can be done in code or by using the Fields Editor in the “Tasks” menu, some ways you can do it in code are:
The RetrieveFields method is a helper that will create PivotGridField objects for all the fields in the bound data source.
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- ASPxPivotGrid1.RetrieveFields();
- }
- }
You can also use the Add method of the Fields collection.
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- ASPxPivotGrid1.Fields.Add("CategoryName", DevExpress.XtraPivotGrid.PivotArea.RowArea);
- ASPxPivotGrid1.Fields.Add("ProductName", DevExpress.XtraPivotGrid.PivotArea.RowArea);
- ASPxPivotGrid1.Fields.Add("ProductSales", DevExpress.XtraPivotGrid.PivotArea.DataArea);
- ASPxPivotGrid1.Fields.Add("ShippedDate", DevExpress.XtraPivotGrid.PivotArea.ColumnArea);
- }
- }
Note: The use of IsPostBack, the page will persist the pivot fields between post backs, if you add duplicate fields during a post back the user can get unexpected results.
While both above methods work by far the easiest way to do this is the Fields Editor I mentioned.
- Open the “ASPxPivotGrid Fields Editor”
- Available from the control task menu
- Or at the Fields link at the bottom of the Properties list for the control
- Click the “Retrieved fields” in the toolbar
- Click OK and run the project
The web page will come up and all the fields will be in the Filter area where a user can move them to the other parts of the grid. While this is good functionality most user will want a default report so we will potion the fields in some default areas.
- Go back to Visual Studio
- Open the Fields Editor like we earlier
- Select the “CategoryName” field
- Change the caption to “Category”
- Change “Area” to “RowArea”
- Select the “ProductName” field
- Change Caption to “Product”
- Change Area to “RowArea”
- Note that AreaIndex value changed to “1” this controls the order of the fields in a given area
- Select the “ProductSales” field
- Change the Caption to “Product Sales”
- Change Area to “DataArea”
- Select the “ShippedDate” field
- Change the Caption to “Product Sales”
- Change Area to “ColumnArea”
At this point we have a default layout for the pivot grid when we run the project at this point you will see the fields in the expected area. The displayed data is quite sparse because we are looking each day in the data where many products only sell a few a week. To make this look better we want show years and months of sales not each day.
- If you have closed the Fields Editor then open it like we earlier
- Select the “ShippedDate” field
- Change the Caption to “Shipped Year”
- Change the GroupInterval to “DateYear”
- Add a new field with the “Add an Item” button on the far left of the toolbar
- Change the Caption to “Shipped Month”
- Change Area to “ColumnArea”
- Change the GroupInterval to “DateMonth”
- Select “ShippedDate” for FieldName (this property has a drop down list of the data fields)
Now we will get the year and each years months displayed when the application is run. The user can collapse & filter on year and month as they like. Also note the other options we had in the GroupInterval property you will see we could have used quarters and may other options.
At this point the markup for the pivot grid looks like:
- <dx:ASPxPivotGrid ID="ASPxPivotGrid1" runat="server" ClientIDMode="AutoID"
- DataSourceID="SqlDataSource1">
- <Fields>
- <dx:PivotGridField ID="fieldCategoryName" Area="RowArea" AreaIndex="0"
- Caption="Category" FieldName="CategoryName">
- </dx:PivotGridField>
- <dx:PivotGridField ID="fieldProductName" Area="RowArea" AreaIndex="1"
- Caption="Product" FieldName="ProductName">
- </dx:PivotGridField>
- <dx:PivotGridField ID="fieldProductSales" Area="DataArea" AreaIndex="0"
- Caption="Product Sales" FieldName="ProductSales">
- </dx:PivotGridField>
- <dx:PivotGridField ID="fieldShippedDate" Area="ColumnArea" AreaIndex="0"
- Caption="Shipped Year" FieldName="ShippedDate" GroupInterval="DateYear">
- </dx:PivotGridField>
- <dx:PivotGridField ID="fieldShippedDate1" Area="ColumnArea" AreaIndex="1"
- Caption="Shipped Month" FieldName="ShippedDate" GroupInterval="DateMonth">
- </dx:PivotGridField>
- </Fields>
- </dx:ASPxPivotGrid>
Now this is looking like what we want (click for animation):


The last thing I want to do is make the Year Shipped and Month Shipped fields a grouped set. This can be useful when you want one or more fields to be associated together, end-users will not be able to separate the fields at runtime.
- Open the Group Editor click on “Groups” link
- Available from the control task menu
- Or at the Fields link at the bottom of the Properties list for the control
- Click on the “Add an item” in the on the top left of the Group Editor
- Select your new group “Group 0” and click “Edit” on the left end of the tool bar
- Move “Shipped Year” and “Shipped Month” from the “Ungrouped Fields” list to the “Grouped Fields”
- Close the Group’s Fields Editor dialog and the Group Editor dialog
- Run the project
You now have a simple ASP.NET application using the ASPxPivotGrid, I hope this quick little walk though will help you get started. The best way to learn what you can do with this control is to start doing.
Please let me know if you have any questions!