# .NET MAUI DataGrid Data Binding: Binding to Dynamic Data

As of Telerik UI for .NET MAUI 6.8.0, the [.NET MAUI DataGrid](https://www.telerik.com/maui-ui/documentation/controls/datagrid/overview) supports binding to any type that implements the standard `IDynamicMetaObjectProvider` DLR interface, such as `DynamicObject` and `ExpandoObject`.

Using Dynamic Data enables you to filter, group, and sort the data inside the DataGrid both through the UI and programmatically.

## Executing Dynamic Data on iOS and MacCatalyst

When you develop applications for Apple devices and plan to bind the DataGrid to dynamic data, you must consider the <a href = "https://learn.microsoft.com/en-us/dotnet/maui/macios/interpreter?view=net-maui-8.0&source=recommendations" target="_blank">security restrictions set by Apple</a>. These restrictions disallow the execution of dynamically generated code on a device. As a result, apps that use `DynamicObject` or `ExpandoObject` will crash in a release build for iOS and Mac Catalyst with a `System.ExecutionEngineException`.
To prevent the exception, use the `MtouchInterpreter` <a href = "https://learn.microsoft.com/en-us/dotnet/maui/macios/interpreter?view=net-maui-8.0&source=recommendations#enable-the-interpreter" target="_blank"> as recommended by Microsoft</a>.

```xml
<PropertyGroup Condition="'$(Configuration)|$(RuntimeIdentifier)'=='Release|maccatalyst-arm64'">
	<MtouchInterpreter>-all,+Telerik.Maui.Controls.dll</MtouchInterpreter>
</PropertyGroup>
```

For more information about these limitations and the suggested solution, see Microsoft's <a href = "https://learn.microsoft.com/en-us/dotnet/maui/macios/interpreter?view=net-maui-8.0&source=recommendations" target="_blank">Mono interpreter on iOS and Mac Catalyst</a> article.

## Binding to DynamicObject

The following example shows how to implement a `DynamicObject` class with Dynamic Language Runtime (DLR) and Common Language Runtime (CLR) fields and bind it to the DataGrid. The standard CLR properties registered in the dynamic type must be exposed through the DLR API.

The model shows a class that derives from `DynamicObject` and contains a CLR property called `Id`. When the DataGrid auto-generates its columns, the `TryGetMember` method of the `DynamicObject` class will be used to fetch the values for each column. You must also implement a specific logic in the method to allow `RadDataGrid` to work with both CLR and DLR data.

**1.** Add the model for the DataGrid:

```C#
public class MyDynamicObject : DynamicObject, INotifyPropertyChanged
{
    private int id;
    private readonly IDictionary<string, object> data = new Dictionary<string, object>();

    // CLR property 
    public int Id
    {
        get => this.id;
        set 
        {
            if (this.id != value)
            {
                this.id = value;
                OnPropertyChanged("Id");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public override IEnumerable<string> GetDynamicMemberNames() => data.Keys;

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        if (binder.Name == nameof(this.Id))
        {
            result = this.Id;
        }
        else
        {
            result = this[binder.Name];
        }

        return true;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        this[binder.Name] = value;
        return true;
    }

    public object this[string columnName]
    {
        get
        {
            if (data.ContainsKey(columnName))
            {
                return data[columnName];
            }
            return null;
        }
        set
        {
            if (!data.ContainsKey(columnName))
            {
                data.Add(columnName, value);
                OnPropertyChanged(columnName);
            }
            else
            {
                if (data[columnName] != value)
                {
                    data[columnName] = value;
                    OnPropertyChanged(columnName);
                }
            }
        }
    }

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
```

**2.** Define the `ViewModel`:

```C#
public class ViewModel
{
    public ViewModel()
    {
        this.Data = new ObservableCollection<MyDynamicObject>();
        for (int i = 0; i < 30; i++)
        {
            var row = new MyDynamicObject() { Id = i };
            for (int j = 0; j < 5; j++)
            {
                row[string.Format("Column {0}", j)] = string.Format("Cell {0} {1}", i, j);
            }

            this.Data.Add(row);
        }
    }

    public ObservableCollection<MyDynamicObject> Data { get; set; }
}
```

**3.** Define the `RadDataGrid`:

```XAML
<telerik:RadDataGrid ItemsSource="{Binding Data}">
    <telerik:RadDataGrid.BindingContext>
        <local:ViewModel />
    </telerik:RadDataGrid.BindingContext>
</telerik:RadDataGrid>
```

**4.** Add the `telerik` namespace:

```XAML
xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"
```

This is the result:

![.NET MAUI DataGrid bound to a DynamicObject data source](https://www.telerik.com/maui-ui/documentation/assets/3b1297bee68bc41aa922b982c943ea16/datagrid-dynamicobject.png)

## Binding to ExpandoObject

**1.** Define the `RadDataGrid` control in XAML:

```XAML
<telerik:RadDataGrid ItemsSource="{Binding Data}">
    <telerik:RadDataGrid.BindingContext>
        <local:ViewModel />
    </telerik:RadDataGrid.BindingContext>
</telerik:RadDataGrid>
```

**2.** Add the `telerik` namespace:

```XAML
xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"
```

**3.** Define the `ViewModel`:

```C#
public class ViewModel
{
    public ViewModel()
    {
        this.Data = new ObservableCollection<ExpandoObject>();
        for (int i = 0; i < 30; i++)
        {
            dynamic item = new ExpandoObject();
            item.Country = "Country " + i;
            item.Capital = "Capital " + i;
            item.Population = i * 10000;

            this.Data.Add(item);
        }
    }

    public ObservableCollection<ExpandoObject> Data { get; set; }
}
```

This is the result:

![.NET MAUI DataGrid bound to an ExpandoObject data source](https://www.telerik.com/maui-ui/documentation/assets/54fbf77c41b51937eeb55d6c5878d465/datagrid-expandoobject.png)

## Additional Resources

* [.NET MAUI DataGrid Product Page](https://www.telerik.com/maui-ui/datagrid)
* [.NET MAUI DataGrid Forum Page](https://www.telerik.com/forums/maui?tagId=1801)
* [Telerik .NET MAUI Blogs](https://www.telerik.com/blogs/mobile-net-maui)
* [Telerik .NET MAUI Roadmap](https://www.telerik.com/support/whats-new/maui-ui/roadmap)

## See Also

* [Filtering UI in the Telerik UI for .NET MAUI DataGrid](https://www.telerik.com/maui-ui/documentation/controls/datagrid/filtering/filtering-ui)
* [Programmatic Filtering in the DataGrid](https://www.telerik.com/maui-ui/documentation/controls/datagrid/filtering/programmatic-filtering)
* [Grouping in the DataGrid](https://www.telerik.com/maui-ui/documentation/controls/datagrid/grouping/overview)
* [Styling the Appearance of the DataGrid](https://www.telerik.com/maui-ui/documentation/controls/datagrid/theming-and-styles/styling)
