# .NET MAUI DataGrid LoadOnDemand Event

You can load new items in the DataGrid by using the `LoadOnDemand` event. The `LoadOnDemand` event handler receives the following parameters:

* The `sender` argument, which is of type `object`, but can be cast to the `RadDataGrid` type.
* A `LoadOnDemandEventArgs` object, which provides the `IsDataLoaded` (`bool`) property, indicating whether the data is loaded.

## Example

The following example demonstrates a sample setup that shows how to use the event:

1. Define the DataGrid in XAML:

```XAML
<telerik:RadDataGrid x:Name="dataGrid"
                     ItemsSource="{Binding Items}"
                     LoadOnDemand="dataGrid_LoadOnDemand"
                     LoadOnDemandMode="Automatic"
                     LoadOnDemandBufferItemsCount="{Binding Source={x:Reference slider}, Path=Value}"/>
```

2. Add the telerik namespace:

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

2. Add sample data:

```C#
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Gender Gender { get; set; }
}
```

3. Define the `ViewModel`:

```C#
public class LoadOnDemandEventViewModel
{
    public LoadOnDemandEventViewModel()
    {
        this.Items = new ObservableCollection<Person>();

        for (int i = 0; i < 20; i++)
        {
            var person = new Person { Name = "Person " + i, Age = i + 18, Gender = i % 2 == 0 ? Gender.Male : Gender.Female };
            this.Items.Add(person);
        }
        
    }

    public ObservableCollection<Person> Items { get; set; }
}
```

4. Handle the `LoadOnDemand` event:

```C#
private void dataGrid_LoadOnDemand(object sender, Telerik.Maui.Controls.DataGrid.LoadOnDemandEventArgs e)
{
    for (int i = 0; i < 15; i++)
    {
        ((sender as RadDataGrid).ItemsSource as ObservableCollection<Person>).Add(new Person() { Name = "Person " + i, Age = i + 18, Gender = i % 2 == 0 ? Gender.Male : Gender.Female });
    }
    e.IsDataLoaded = true;
}
```

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

## See Also

* [Load On Demand Overview](https://www.telerik.com/maui-ui/documentation/controls/datagrid/load-on-demand/overview)
* [Load On Demand Collection](https://www.telerik.com/maui-ui/documentation/controls/datagrid/load-on-demand/collection)
* [Load On Demand Command](https://www.telerik.com/maui-ui/documentation/controls/datagrid/load-on-demand/command)
* [Load On Demand Styling](https://www.telerik.com/maui-ui/documentation/controls/datagrid/load-on-demand/styling)
