New to Telerik UI for WinFormsStart a free 30-day trial

How to create calculated field using another calculated field

Updated on Sep 24, 2025

Environment

Product VersionProductAuthor
2022.2.622RadPivotGrid for WinFormsDinko Krastev

Description

An example demonstrating how to get the value of a calculated field in another calculated field.

Solution

The RequiredField static class exposes two methods to get fields in RadPivotGrid: RequiredField.ForProperty() and RequiredField.ForCalculatedField(). Using the second method we can get another calculated field value.

pivotgrid-custom-calculatedfields 001

Sample Implementation

  1. First we are going to populate our RadPivotGrid with sample data.
C#

public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    public RadForm1()
    {
        InitializeComponent();
        LocalDataSourceProvider dataProvider = new LocalDataSourceProvider();
        this.radPivotGrid1.DataProvider = dataProvider;
        dataProvider.BeginInit();
        dataProvider.RowGroupDescriptions.Add(new PropertyGroupDescription() { PropertyName = "Name" });
        dataProvider.ColumnGroupDescriptions.Add(new DateTimeGroupDescription() { PropertyName = "Date", Step = DateTimeStep.Month });
        dataProvider.AggregateDescriptions.Add(new PropertyAggregateDescription() { PropertyName = "Quantity" });
        dataProvider.EndInit();
		
        dataProvider.ItemsSource = GeneratePivotData();
    }

    private static IList<Product> GeneratePivotData()
    {
        IList<Product> PivotData = new List<Product>()
        {
            new Product() { Name = "Pen", Date = new DateTime(2012,1,1,0,0,0), Price = 10.40, Quantity = 148 },
            new Product() { Name = "Pen", Date = new DateTime(2012,2,1,0,0,0), Price = 10.99, Quantity = 122 },
            new Product() { Name = "Pen", Date = new DateTime(2012,3,1,0,0,0), Price = 11.24, Quantity = 80 },
            new Product() { Name = "Pen", Date = new DateTime(2012,4,1,0,0,0), Price = 11.24, Quantity = 90 },
            new Product() { Name = "Pen", Date = new DateTime(2012,5,1,0,0,0), Price = 11.14, Quantity = 140 },
            new Product() { Name = "Pen", Date = new DateTime(2012,6,1,0,0,0), Price = 10.89, Quantity = 162 },
            new Product() { Name = "Pen", Date = new DateTime(2012,7,1,0,0,0), Price = 10.89, Quantity = 181 },
            new Product() { Name = "Pen", Date = new DateTime(2012,8,1,0,0,0), Price = 10.88, Quantity = 180 },
            new Product() { Name = "Pen", Date = new DateTime(2012,9,1,0,0,0), Price = 11.00, Quantity = 116 },
            new Product() { Name = "Pen", Date = new DateTime(2012,10,1,0,0,0), Price = 10.99, Quantity = 128 },
            new Product() { Name = "Pen", Date = new DateTime(2012,11,1,0,0,0), Price = 10.95, Quantity = 145 },
            new Product() { Name = "Pen", Date = new DateTime(2012,12,1,0,0,0), Price = 10.45, Quantity = 189 },
            new Product() { Name = "Pencil", Date = new DateTime(2012,1,1,0,0,0), Price = 5.22, Quantity = 100 },
            new Product() { Name = "Pencil", Date = new DateTime(2012,2,1,0,0,0), Price = 5.99, Quantity = 85 },
            new Product() { Name = "Pencil", Date = new DateTime(2012,3,1,0,0,0), Price = 6.04, Quantity = 80 },
            new Product() { Name = "Pencil", Date = new DateTime(2012,4,1,0,0,0), Price = 6.28, Quantity = 72 },
            new Product() { Name = "Pencil", Date = new DateTime(2012,5,1,0,0,0), Price = 6.12, Quantity = 99 },
            new Product() { Name = "Pencil", Date = new DateTime(2012,6,1,0,0,0), Price = 6.59, Quantity = 40 },
            new Product() { Name = "Pencil", Date = new DateTime(2012,7,1,0,0,0), Price = 6.29, Quantity = 68 },
            new Product() { Name = "Pencil", Date = new DateTime(2012,8,1,0,0,0), Price = 5.99, Quantity = 95 },
            new Product() { Name = "Pencil", Date = new DateTime(2012,9,1,0,0,0), Price = 5.89, Quantity = 120 },
            new Product() { Name = "Pencil", Date = new DateTime(2012,10,1,0,0,0), Price = 5.99, Quantity = 105 },
            new Product() { Name = "Pencil", Date = new DateTime(2012,11,1,0,0,0), Price = 5.96, Quantity = 111 },
            new Product() { Name = "Pencil", Date = new DateTime(2012,12,1,0,0,0), Price = 5.99, Quantity = 108 },
            new Product() { Name = "Notebook", Date = new DateTime(2012,1,1,0,0,0), Price = 22.86, Quantity = 88 },
            new Product() { Name = "Notebook", Date = new DateTime(2012,2,1,0,0,0), Price = 23.02, Quantity = 95},
            new Product() { Name = "Notebook", Date = new DateTime(2012,3,1,0,0,0), Price = 23.22, Quantity = 102 },
            new Product() { Name = "Notebook", Date = new DateTime(2012,4,1,0,0,0), Price = 21.99, Quantity = 95},
            new Product() { Name = "Notebook", Date = new DateTime(2012,5,1,0,0,0), Price = 22.45, Quantity = 84},
            new Product() { Name = "Notebook", Date = new DateTime(2012,6,1,0,0,0), Price = 22.56, Quantity = 96 },
            new Product() { Name = "Notebook", Date = new DateTime(2012,7,1,0,0,0), Price = 22.88, Quantity = 88},
            new Product() { Name = "Notebook", Date = new DateTime(2012,8,1,0,0,0), Price = 22.42, Quantity = 99},
            new Product() { Name = "Notebook", Date = new DateTime(2012,9,1,0,0,0), Price = 22.56, Quantity = 111},
            new Product() { Name = "Notebook", Date = new DateTime(2012,10,1,0,0,0), Price = 22.18, Quantity = 102 },
            new Product() { Name = "Notebook", Date = new DateTime(2012,11,1,0,0,0), Price = 22.93, Quantity = 105 },
            new Product() { Name = "Notebook", Date = new DateTime(2012,12,1,0,0,0), Price = 22.89, Quantity = 122 },
        };

        return PivotData;
    }
}

public class Product
{
    public string Name { get; set; }
    public int Quantity { get; set; }
    public double Price { get; set; }
    public DateTime Date { get; set; }
}

	
  1. Next step is to create our custom-calculated fields.
C#

	public class CommissionCalculatedField : CalculatedField
	{
		private RequiredField sellPriceCalculatedField;
		public CommissionCalculatedField()
		{
			this.Name = "Commision";
			this.sellPriceCalculatedField = RequiredField.ForCalculatedField("SellPrice");

		}
		protected override IEnumerable<RequiredField> RequiredFields()
		{
			yield return this.sellPriceCalculatedField;
		}
			
		protected override AggregateValue CalculateValue(IAggregateValues aggregateValues)
		{      
			var quantity = aggregateValues.GetAggregateValue(sellPriceCalculatedField);
			if (quantity.IsError())
			{
				return quantity;
			}
			double extendedPrice = quantity.ConvertOrDefault<double>();
			return new DoubleAggregateValue(extendedPrice * 0.1);            
		}
	}

	public class SellPriceCalculatedField : CalculatedField
	{
		private RequiredField sellPriceCalculatedField;
		public SellPriceCalculatedField()
		{
			this.Name = "SellPrice";
			this.sellPriceCalculatedField = RequiredField.ForProperty("Price");
		}
		protected override IEnumerable<RequiredField> RequiredFields()
		{
			yield return this.sellPriceCalculatedField;
		}

		protected override AggregateValue CalculateValue(IAggregateValues aggregateValues)
		{
			var aggregateValue = aggregateValues.GetAggregateValue(this.sellPriceCalculatedField);
			if (aggregateValue.IsError())
			{
				return aggregateValue;
			}
			double extendedPrice = aggregateValue.ConvertOrDefault<double>();
			return new DoubleAggregateValue(extendedPrice * 1.5);            
		}
	}
	
	
  1. What's left is to add the above-calculated fields to the RadPivotGrid control.
C#

	public RadForm1()
	{
		InitializeComponent();
		LocalDataSourceProvider dataProvider = new LocalDataSourceProvider();
		this.radPivotGrid1.DataProvider = dataProvider;
		dataProvider.BeginInit();
		dataProvider.RowGroupDescriptions.Add(new PropertyGroupDescription() { PropertyName = "Name" });
		dataProvider.ColumnGroupDescriptions.Add(new DateTimeGroupDescription() { PropertyName = "Date", Step = DateTimeStep.Month });
		dataProvider.AggregateDescriptions.Add(new PropertyAggregateDescription() { PropertyName = "Quantity" });
		dataProvider.EndInit();

		using (radPivotGrid1.PivotGridElement.DeferRefresh())
		{
			SellPriceCalculatedField sellPriceCalculatedField = new SellPriceCalculatedField();
			sellPriceCalculatedField.Name = "SellPrice";
			((LocalDataSourceProvider)this.radPivotGrid1.DataProvider).CalculatedFields.Add(sellPriceCalculatedField);

			CommissionCalculatedField calculatedField = new CommissionCalculatedField();
			calculatedField.Name = "Commision";
			((LocalDataSourceProvider)this.radPivotGrid1.DataProvider).CalculatedFields.Add(calculatedField);
		}
		
		dataProvider.ItemsSource = GeneratePivotData();
	}
	
In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support