# .NET MAUI DataGrid LoadMoreData Command

The `LoadMoreData` command is another alternative for loading data on demand. This alternative is suitable for MVVM scenarios.

## Example

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

1. Define the DataGrid in XAML:

```XAML
<telerik:RadDataGrid x:Name="dataGrid"
                     ItemsSource="{Binding Items}"
                     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 LoadMoreDataCommandViewModel
{
    public LoadMoreDataCommandViewModel()
    {
        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. Create the custom command:

```C#
public class CustomLoadMoreDataCommand : DataGridCommand
{
    public CustomLoadMoreDataCommand()
    {
        this.Id = DataGridCommandId.LoadMoreData;
    }

    public override bool CanExecute(object parameter)
    {
        return true;
    }

    public async override void Execute(object parameter)
    {
        if (parameter == null)
        {
            return;
        }

        ((LoadOnDemandContext)parameter).ShowLoadOnDemandLoadingIndicator();
        
        await System.Threading.Tasks.Task.Delay(1500);
        var viewModel = this.Owner.BindingContext as LoadMoreDataCommandViewModel;
        if (viewModel != null)
        {
            for (int i = 0; i < 10; i++)
            {
                viewModel.Items.Add(new Person { Name = "Person " + i, Age = i + 18, Gender = i % 2 == 0 ? Gender.Male : Gender.Female });
            }
        }
        ((LoadOnDemandContext)parameter).HideLoadOnDemandLoadingIndicator();
    }
}
```

> important Invoking the `ShowLoadOnDemandLoadingIndicator` and `HideLoadOnDemandLoadingIndicators` methods is required. Without calling these methods, the `BusyIndicator` used for the functionality is not visualized.

5. Add the custom command to the `Commands` collection of the DataGrid:

```C#
this.dataGrid.Commands.Add(new CustomLoadMoreDataCommand());
```

> important For DataGrid LoadMoreData command 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 Event](https://www.telerik.com/maui-ui/documentation/controls/datagrid/load-on-demand/event)
* [Load On Demand Styling](https://www.telerik.com/maui-ui/documentation/controls/datagrid/load-on-demand/styling)
* [DataGrid Commands](https://www.telerik.com/maui-ui/documentation/controls/datagrid/commands/overview)
