This is a migrated thread and some comments may be shown as answers.

Pivot Grid Field List dont show CalculatedFields

1 Answer 147 Views
PivotGrid and PivotFieldList
This is a migrated thread and some comments may be shown as answers.
José
Top achievements
Rank 1
José asked on 20 Apr 2015, 07:31 PM

im trying to add a custom calculated field in my pivot following the intructions you give here 

http://www.telerik.com/help/winforms/pivotgrid-calculated-fields.html

 

here is my code and it doesnt work, any help?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.Pivot.Core;
using Telerik.Pivot.Core.Aggregates;
using Telerik.WinControls;

namespace GAR.Ventas
{
  public partial class frmPivot2 : Telerik.WinControls.UI.RadForm
  {
    public frmPivot2()
    {
      InitializeComponent();
    }

    public class Venta
    {
      public string Vendedor { get; set; }
      public string Cliente { get; set; }
      public string Zona { get; set; }
      public string Articulo { get; set; }
      public decimal Cantidad { get; set; }
      public decimal Precio { get; set; }
      public decimal Total { get { return Cantidad * Precio; } }
      public decimal Costo { get; set; }
      public decimal Margen { get { return Precio - Costo; } }
      public DateTime Fecha { get; set; }
    }

    public class Articulo
    {
      public string Descripcion { get; set; }
      public decimal Precio { get; set; }
      public decimal Costo { get { return Precio - (Precio * (SGS.Utils.convert.ToDecimal(20) / SGS.Utils.convert.ToDecimal(100))); } }
    }

    public class Cliente
    {
      public string Nombre { get; set; }
      public string Zona { get; set; }
    }


    public List<Venta> Ventas { get; set; }


    private void frmPivot2_Load(object sender, EventArgs e)
    {
      LocalDataSourceProvider provider;

      List<Cliente> Clientes = new List<Cliente>();
      List<Articulo> Articulos = new List<frmPivot2.Articulo>();
      List<Cliente> Vendedores = new List<Cliente>();

      Clientes.Add(new Cliente { Nombre = "JOSE PENA", Zona = "SANTO DOMINGO" });
      Clientes.Add(new Cliente { Nombre = "JUAN PEREZ", Zona = "SANTO DOMINGO" });
      Clientes.Add(new Cliente { Nombre = "JULIAN MONTERO", Zona = "SANTO DOMINGO" });

      Clientes.Add(new Cliente { Nombre = "SAMUEL AURELIO", Zona = "SANTIAGO" });
      Clientes.Add(new Cliente { Nombre = "SERGIO GUZMAN", Zona = "SANTIAGO" });
      Clientes.Add(new Cliente { Nombre = "JUAN CARLOS TEJEDA", Zona = "SANTIAGO" });
      Clientes.Add(new Cliente { Nombre = "LUIS CROUSSET", Zona = "SANTIAGO" });

      Clientes.Add(new Cliente { Nombre = "JOSE PERDOMO", Zona = "LA ROMANA" });
      Clientes.Add(new Cliente { Nombre = "ERIK GUTIERRES", Zona = "LA ROMANA" });
      Clientes.Add(new Cliente { Nombre = "ERICK GOMEZ", Zona = "LA ROMANA" });

      Articulos.Add(new Articulo { Descripcion = "CAJA DE BOLA", Precio = 100 });
      Articulos.Add(new Articulo { Descripcion = "JUEGO DE GOMAS AX100", Precio = SGS.Utils.convert.ToDecimal(123.44) });
      Articulos.Add(new Articulo { Descripcion = "CUARTO ACEITE CASTROL", Precio = SGS.Utils.convert.ToDecimal(1830.22) });
      Articulos.Add(new Articulo { Descripcion = "MICA DERECHA HONDA CIVIC", Precio = SGS.Utils.convert.ToDecimal(289.44) });
      Articulos.Add(new Articulo { Descripcion = "PANTALLA DELANTERA", Precio = SGS.Utils.convert.ToDecimal(2400.00) });
      Articulos.Add(new Articulo { Descripcion = "TAPA BOCINAS", Precio = SGS.Utils.convert.ToDecimal(1645.00) });
      Articulos.Add(new Articulo { Descripcion = "BOLA ESFERICA HONDA", Precio = SGS.Utils.convert.ToDecimal(999.99) });
      Articulos.Add(new Articulo { Descripcion = "FRENTIL NISSAN", Precio = SGS.Utils.convert.ToDecimal(1255) });
      Articulos.Add(new Articulo { Descripcion = "PUERTA DERECHA CIVIC 2000", Precio = SGS.Utils.convert.ToDecimal(1320.99) });
      Articulos.Add(new Articulo { Descripcion = "MONITOR 24", Precio = SGS.Utils.convert.ToDecimal(2800) });
      Articulos.Add(new Articulo { Descripcion = "LAPTOP ACER", Precio = SGS.Utils.convert.ToDecimal(23000.00) });


      Vendedores.Add(new Cliente { Nombre = "PEDRO CASTILLO" });
      Vendedores.Add(new Cliente { Nombre = "ERNESTO PASCUAL" });
      Vendedores.Add(new Cliente { Nombre = "CARLOS PAULINO" });
      Vendedores.Add(new Cliente { Nombre = "CARLO ACOSTA" });
      Vendedores.Add(new Cliente { Nombre = "GERONIMO MARTE" });
      Vendedores.Add(new Cliente { Nombre = "ANTHONY REYES" });

      Ventas = new List<Venta>();

      int idArticulo = 0;
      int idCliente = 0;
      int idVendedor = 0;
      Random rnd = new Random();
      for (int i = 0; i <= 10000; i++)
      {
        idArticulo = rnd.Next(1, 11);
        idCliente = rnd.Next(1, 10);
        idVendedor = rnd.Next(1, 6);

        Ventas.Add(new Venta
        {
          Articulo = Articulos[idArticulo].Descripcion
          ,
          Cantidad = rnd.Next(1, 100)
          ,
          Cliente = Clientes[idCliente].Nombre
          ,
          Zona = Clientes[idCliente].Zona
          ,
          Vendedor = Vendedores[idVendedor].Nombre
          ,
          Precio = Articulos[idArticulo].Precio
          ,
          Costo = Articulos[idArticulo].Costo
          ,
          Fecha = DateTime.Now.AddDays(rnd.Next(1, 200) * -1)
        });
      }

      provider = new LocalDataSourceProvider() { ItemsSource = Ventas  };
      provider.Culture = new System.Globalization.CultureInfo("es-DO");

      this.radPivotGrid1.DataProvider = provider;


      using (radPivotGrid1.PivotGridElement.DeferRefresh())
      {
        CommissionCalculatedField calculatedField = new CommissionCalculatedField();
        calculatedField.Name = "Commission";

        ((LocalDataSourceProvider)this.radPivotGrid1.DataProvider).CalculatedFields.Add(calculatedField);
      }




      this.radPivotGrid1.PivotGridElement.DataProvider.Refresh();
    }


    #region PRUEBA 1
    public class CommissionCalculatedField : CalculatedField
    {
      private RequiredField extendPriceField;

      public CommissionCalculatedField()
      {
        this.Name = "Commission";
        this.extendPriceField = RequiredField.ForProperty("Precio");
      }

      protected override IEnumerable<RequiredField> RequiredFields()
      {
        yield return this.extendPriceField;
      }

      protected override AggregateValue CalculateValue(IAggregateValues aggregateValues)
      {
        var aggregateValue = aggregateValues.GetAggregateValue(this.extendPriceField);
        if (aggregateValue.IsError())
        {
          return aggregateValue;
        }

        double extendedPrice = aggregateValue.ConvertOrDefault<double>();
        if (extendedPrice > 15000)
        {
          return new DoubleAggregateValue(extendedPrice * 0.1);
        }

        return null;
      }
    }
    #endregion
  }
}

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 23 Apr 2015, 08:46 AM
Hello Jose,

Thank you for writing.

The important part here is to add the custom field before setting the ItemsSource property (it is mentioned at the end of the documentation article):
LocalDataSourceProvider ldp = new LocalDataSourceProvider();
  
using (radPivotGrid1.PivotGridElement.DeferRefresh())
{
    CommissionCalculatedField calculatedField = new CommissionCalculatedField();
    calculatedField.Name = "Commission";
  
    ldp.CalculatedFields.Add(calculatedField);
}
  
ldp.ItemsSource = CreateDataTable();

I hope this helps. Should you have any other questions do not hesitate to ask.
 
Regards,
Dimitar
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
PivotGrid and PivotFieldList
Asked by
José
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or