# .NET MAUI DataGrid Delegate Aggregate Descriptor

The `DelegateAggregateDescriptor` allows you to define property lookup logic and a custom function that are applied over the property values of the [.NET MAUI DataGrid](https://www.telerik.com/maui-ui/documentation/controls/datagrid/overview), which accumulates an aggregated result based on the component data.

To set up the `DelegateAggregateDescriptor`, use the following properties:

* `ValueLookup`—Defines an `IKeyLookup` instance, which retrieves the value from the underlying `ViewModel` that is used for computing the aggregated value.

* `Function`—Defines an `IAggregateFunction` instance that performs the aggregation of the values as specified by the `ValueLookup` property.

* `Format`—Defines the string format that will be applied over the aggregated value.

The `IAggregateFunction` interface exposes the following methods:

* `GetValue()`—Gets the computed value.

* `Accumulate(object value)`—Applies the function logic to the provided value (the provided value is the extracted value from the `ViewModel`).

* `Merge(IAggregateFunction aggregateFunction)`—Merges this function with another one. Used when calculating Grand Totals.

* `Update(object oldValue, object newValue)`—Updates the aggregate function by removing the contribution of an old value and adding a new value. This allows for efficient recalculation when data items are modified without recomputing the entire aggregation.

* `Clone()`—Clones the current instance of the `IAggregateFunction`.

The following example uses the `DelegateAggregateDescriptor` and a custom implementation for a `SumIf` function which sums the values in a range that meet a certain criteria:

**1.** Create a class that inherits from the `IKeyLookup` interface. It returns the values of a `Price` property declared in the business model that is of type `double`.

```C#
public class SumIfKeyLookUp : IKeyLookup
{
    public object GetKey(object instance) => ((Data)instance).Price; 
}
```

**2.** Declare a class that inherits from the `IAggregateFunction` interface. This class will contain our logic for the `SumIf` function which we will later implement through XAML:

```C#
public class SumIfAggregateFunction : IAggregateFunction
{
    private double value;
    public double GreaterThanValue { get; set; }

    public object GetValue() => $"SumIf (Price > {this.GreaterThanValue}): " + string.Format("{0:C}", this.value);

    public IAggregateFunction Clone() => new SumIfAggregateFunction() { GreaterThanValue = this.GreaterThanValue };

    public void Accumulate(object value)
    {
        var price = (double)value;
        if (price > this.GreaterThanValue)
        {
            this.value += price;
        }
    }

    public void Merge(IAggregateFunction aggregateFunction)
    {
        var myFunction = aggregateFunction as SumIfAggregateFunction;
        if (myFunction != null)
        {
            this.value += myFunction.value;
        }
    }

    public void Update(object oldValue, object newValue)
    {
        var oldPrice = (double)oldValue;
        var newPrice = (double)newValue;

        if (oldPrice > this.GreaterThanValue)
        {
            this.value -= oldPrice;
        }

        if (newPrice > this.GreaterThanValue)
        {
            this.value += newPrice;
        }
    }
}
```

**3.** Declare the `DelegateAggregateDescriptor` in XAML.

```XAML
<telerik:RadDataGrid x:Name="dataGrid"
                     UserGroupMode="Disabled"
                     AutoGenerateColumns="False"
                     UserEditMode="Cell"
                     ShowColumnFooters="True">
    <telerik:RadDataGrid.Columns>
        <telerik:DataGridTextColumn PropertyName="Name" />
        <telerik:DataGridNumericalColumn PropertyName="Price"
                                         CellContentFormat="{}{0:C}">
            <telerik:DataGridNumericalColumn.AggregateDescriptors>
                <telerik:DelegateAggregateDescriptor>
                    <telerik:DelegateAggregateDescriptor.ValueLookup>
                        <local:SumIfKeyLookUp/>
                    </telerik:DelegateAggregateDescriptor.ValueLookup>
                    <telerik:DelegateAggregateDescriptor.Function>
                        <local:SumIfAggregateFunction GreaterThanValue="100"/>
                    </telerik:DelegateAggregateDescriptor.Function>
                </telerik:DelegateAggregateDescriptor>
            </telerik:DataGridNumericalColumn.AggregateDescriptors>
        </telerik:DataGridNumericalColumn>
    </telerik:RadDataGrid.Columns>
</telerik:RadDataGrid>
```

**4.** Define a sample data:

```C#
public class Data : NotifyPropertyChangedBase
{
    private string name;
    private double price;
    private double deliveryPrice;
    private int quantity;

    public string Name
    {
        get => this.name;
        set
        {
            if (value != this.name)
            {
                this.name = value;
                this.OnPropertyChanged();
            }
        }
    }

    public double Price
    {
        get => this.price;
        set
        {
            if (value != this.price)
            {
                this.price = value;
                this.OnPropertyChanged();
            }
        }
    }

    public double DeliveryPrice
    {
        get => this.deliveryPrice;
        set
        {
            if (value != this.deliveryPrice)
            {
                this.deliveryPrice = value;
                this.OnPropertyChanged();
            }
        }
    }

    public int Quantity
    {
        get => this.quantity;
        set
        {
            if (value != this.quantity)
            {
                this.quantity = value;
                this.OnPropertyChanged();
            }
        }
    }
}
```

**5.** Set a source to the `RadDataGrid.ItemsSource` property:

```C#
this.dataGrid.ItemsSource = new List<Data>
{
    new Data { Name = "KeyBoard", Price = 24.6, DeliveryPrice = 2, Quantity = 32 },
    new Data { Name = "Mouse", Price = 30.9, DeliveryPrice = 2, Quantity = 54 },
    new Data { Name = "Video Card", Price = 760.7, DeliveryPrice = 3, Quantity = 17 },
    new Data { Name = "Motherboard", Price = 210.4, DeliveryPrice = 4, Quantity = 12 },
    new Data { Name = "SSD", Price = 42.9, DeliveryPrice = 3, Quantity = 88 },
    new Data { Name = "RAM", Price = 50, DeliveryPrice = 4, Quantity = 126 }
};
```

The following image shows the end result.

![.NET MAUI DataGrid DelegateAggregateDescriptor aggregate results example on Windows](https://www.telerik.com/maui-ui/documentation/assets/bfebaf3e541226cf8ca5149f0624e06f/datagrid-delegate-aggregate-windows.png)

> important For the DataGrid `DelegateAggregateDescriptor` example refer to the [SDKBrowser Demo application](https://www.telerik.com/maui-ui/documentation/demos-and-sample-apps/sdkbrowser-app) and navigate to the **DataGrid > Aggregates** category.

## See Also

* [Property Aggregate Descriptor](https://www.telerik.com/maui-ui/documentation/controls/datagrid/aggregates/property-aggregate-descriptor)
