aspnet-7311-components-pivot-grid-examples-binding-to-data-how-to-add-unbound-fields.md
The following example shows how to add unbound fields to the ASPxPivotGrid.
The ‘DiscountAmount’ unbound field’s values are calculated using the PivotGridFieldBase.UnboundExpression property, while the ‘PriceWithDiscount’ unbound field is populated by handling the ASPxPivotGrid.CustomUnboundFieldData event.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="AddUnboundField._Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxPivotGrid.v13.1, Version=13.1.5.0,
Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxPivotGrid"
TagPrefix="dx" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxPivotGrid ID="ASPxPivotGrid1" runat="server" DataSourceID="AccessDataSource1"
OnCustomUnboundFieldData="CustomUnboundFieldData">
<Fields>
<dx:PivotGridField ID="fieldSalesperson" Area="RowArea"
AreaIndex="0" FieldName="Salesperson">
</dx:PivotGridField>
<dx:PivotGridField ID="fieldCountry" Area="ColumnArea"
AreaIndex="0" FieldName="Country">
</dx:PivotGridField>
<dx:PivotGridField ID="fieldExtendedPrice" Area="DataArea"
AreaIndex="0" FieldName="ExtendedPrice">
</dx:PivotGridField>
<dx:PivotGridField ID="fieldDiscount" Area="DataArea"
AreaIndex="1" FieldName="Discount">
</dx:PivotGridField>
<dx:PivotGridField ID="fieldDiscountAmount" Area="DataArea"
UnboundExpression="[ExtendedPrice]*[Discount]"
AreaIndex="2" UnboundType="Decimal"
Caption="Discount Amount">
</dx:PivotGridField>
<dx:PivotGridField ID="fieldPriceWithDiscount" Area="DataArea"
AreaIndex="3" UnboundType="Decimal"
FieldName="PriceWithDiscount"
Caption="Price with Discount">
</dx:PivotGridField>
</Fields>
</dx:ASPxPivotGrid>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/nwind.mdb"
SelectCommand="SELECT [Salesperson], [Country],
[ExtendedPrice], [Discount]
FROM [Invoices]">
</asp:AccessDataSource>
</div>
</form>
</body>
</html>
using System;
using System.Web.UI;
using DevExpress.Web.ASPxPivotGrid;
namespace AddUnboundField {
public partial class _Default : Page {
protected void Page_Load(object sender, EventArgs e) {
}
protected void CustomUnboundFieldData(object sender, CustomFieldDataEventArgs e) {
if (e.Field.FieldName != "PriceWithDiscount") return;
decimal extPrice = Convert.ToDecimal(e.GetListSourceColumnValue("ExtendedPrice"));
decimal discount = Convert.ToDecimal(e.GetListSourceColumnValue("Discount"));
e.Value = extPrice * (1 - discount);
}
}
}
<%@ Page Language="vb" AutoEventWireup="true" CodeBehind="Default.aspx.vb"
Inherits="AddUnboundField._Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxPivotGrid.v13.1, Version=13.1.4.0,
Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxPivotGrid"
TagPrefix="dx" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxPivotGrid ID="ASPxPivotGrid1" runat="server" DataSourceID="AccessDataSource1"
OnCustomUnboundFieldData="CustomUnboundFieldData">
<Fields>
<dx:PivotGridField ID="fieldSalesperson" Area="RowArea"
AreaIndex="0" FieldName="Salesperson">
</dx:PivotGridField>
<dx:PivotGridField ID="fieldCountry" Area="ColumnArea"
AreaIndex="0" FieldName="Country">
</dx:PivotGridField>
<dx:PivotGridField ID="fieldExtendedPrice" Area="DataArea"
AreaIndex="0" FieldName="ExtendedPrice">
</dx:PivotGridField>
<dx:PivotGridField ID="fieldDiscount" Area="DataArea"
AreaIndex="1" FieldName="Discount">
</dx:PivotGridField>
<dx:PivotGridField ID="fieldDiscountAmount" Area="DataArea"
UnboundExpression="[ExtendedPrice]*[Discount]"
AreaIndex="2" UnboundType="Decimal"
Caption="Discount Amount">
</dx:PivotGridField>
<dx:PivotGridField ID="fieldPriceWithDiscount" Area="DataArea"
AreaIndex="3" UnboundType="Decimal"
FieldName="PriceWithDiscount"
Caption="Price with Discount">
</dx:PivotGridField>
</Fields>
</dx:ASPxPivotGrid>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/nwind.mdb"
SelectCommand="SELECT [Salesperson], [Country],
[ExtendedPrice], [Discount]
FROM [Invoices]">
</asp:AccessDataSource>
</div>
</form>
</body>
</html>
Imports Microsoft.VisualBasic
Imports System
Imports System.Web.UI
Imports DevExpress.Web.ASPxPivotGrid
Namespace AddUnboundField
Partial Public Class _Default
Inherits Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub CustomUnboundFieldData(ByVal sender As Object, ByVal e As CustomFieldDataEventArgs)
If e.Field.FieldName <> "PriceWithDiscount" Then
Return
End If
Dim extPrice As Decimal = Convert.ToDecimal(e.GetListSourceColumnValue("ExtendedPrice"))
Dim discount As Decimal = Convert.ToDecimal(e.GetListSourceColumnValue("Discount"))
e.Value = extPrice * (1 - discount)
End Sub
End Class
End Namespace
See Also
Bind Pivot Grid Fields to Calculated Expressions
How to use the other cell's values in the current cell value calculation