How to: Add an Unbound Field to Supply Additional Data

The code snippet below demonstrates how to create an unbound field in code and supply it with data using the PivotGridControl.CustomUnboundFieldData event. In this example, extended price values are calculated manually using the formula UnitPrice * Quantity * (1-Discount).

PivotGridContol.CustomUnboundFieldData_ex

using DevExpress.XtraPivotGrid;

// ...
        public Form1() {
            // ...
            pivotGridControl1.CustomUnboundFieldData += PivotGridControl1_CustomUnboundFieldData;

            PivotGridField fieldExtendedPrice = new PivotGridField() { Caption = "Extended Price", Area = PivotArea.DataArea };
            fieldExtendedPrice.UnboundFieldName = "fieldExtendedPrice";
            fieldExtendedPrice.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;

            pivotGridControl1.Fields.Add(fieldExtendedPrice);
        }

        private void PivotGridControl1_CustomUnboundFieldData(object sender, CustomFieldDataEventArgs e) {
            if (e.Field.UnboundFieldName == "fieldExtendedPrice") {
                decimal unitPrice = Convert.ToDecimal(e.GetListSourceColumnValue(fieldUnitPrice.ExpressionFieldName));
                int qty = Convert.ToInt32(e.GetListSourceColumnValue(fieldQuantity.ExpressionFieldName));
                decimal discount = Convert.ToDecimal(e.GetListSourceColumnValue(fieldDiscount.ExpressionFieldName));
                e.Value = unitPrice * qty * (1 - discount);
            }
        }
    }
}