New to Telerik UI for .NET MAUI? Start a free 30-day trial

.NET MAUI DataGrid Load On Demand

In specific cases you may need to load data in the RadDataGrid when the control is already displayed as this can improve the performance and save computing resources. Loading a large data set on a mobile device has its challenges. One of the most popular approaches is using incremental data loading the moment the items need to be visualized.

Modes

The DataGrid offers two loading modes which are present in the LoadOnDemandMode enumeration:

  • Automatic—The load-on-demand mechanism is activated when you scroll down near the last item present in the view port.

    You can control when the items will start loading more precisely by setting the LoadOnDemandBufferItemsCount property. It indicates at what point the additional items will start loading. For example, setting it to 20 will cause the new items to be loaded when you have scrolled the DataGrid, so that only 20 of the originally loaded items are left below.

  • Manual—A Load More button is present at the bottom of the DataGrid. Clicking it will load additional items based on the approach you have chosen for loading the items (through the event, the command, or the collection).

Approaches

The DataGrid supports three different options for using its load-on-demand feature. You can choose the most convenient for you based on your application requirements:

LoadOnDemandCollection

To use this approach, you have to feed the RadDataGrid with a collection of type LoadOnDemandCollection. It is a generic type, so you need to point the type of objects it will contain. It extends the ObservableCollection<T> class and expects a Func<CancellationToken, IEnumerable> in the constructor.

The following example demonstrates a simple setup that shows how to use the collection:

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

    return list;
});

In the example, the Items property is declared as follows:

public LoadOnDemandCollection<Person> Items { get; set; }

LoadOnDemand Event

You can load new items by utilizing the LoadOnDemand event. It uses LoadOnDemandEventArgs arguments through which you need to indicate when the data is loaded using the IsDataLoaded(bool) property.

private void dataGrid_LoadOnDemand(object sender, Telerik.Maui.Controls.Compatibility.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;
}

LoadMoreData Command

The LoadMoreData command is another alternative which you can use and which is suitable for MVVM scenarios.

The following example demonstrates how to create the command.

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();
    }
}

Eventually, you need to add this custom command to the Commands collection of the DataGrid.

this.dataGrid.Commands.Add(new CustomLoadMoreDataCommand());

Invoking the ShowLoadOnDemandLoadingIndicator and HideLoadOnDemandLoadingIndicators is a notable part as without calling these methods, the BusyIndicator used for the functionality will not be visualized.

Styling

Besides the different approaches for loading the data, RadDataGrid exposes several mechanisms related to the styling of the functionality which you can use according to the approach you have chosen.

LoadOnDemandRowStyle

The LoadOnDemandRowStyle property can be used to style the appearance of the row that contains the Load More button when the LoadOnDemandMode is Manual.

The custom style is of type DataGridLoadOnDemandRowStyle:

<telerik:DataGridLoadOnDemandRowStyle x:Key="CustomDataGridLoadOnDemandRowStyle" 
                                      BackgroundColor="LightYellow"
                                      BorderColor="LightBlue"
                                      IndicatorAnimationColor="Orange"
                                      IndicatorAnimationType="Animation5"
                                      HorizontalTextAlignment="Center"
                                      VerticalTextAlignment="Center"
                                      OverlayOpacity="0.5"
                                      Text="Some Text"
                                      TextFontSize="16"
                                      TextColor="DarkGray"
                                      TextFontFamily="Times New Roman"/>

You have to set it to the LoadOnDemandRowStyle property of the DataGrid:

<telerik:RadDataGrid x:Name="dataGrid" ItemsSource="{Binding Items}"
                     LoadOnDemand="dataGrid_LoadOnDemand"
                     LoadOnDemandMode="Manual"
                     LoadOnDemandRowStyle="{StaticResource CustomDataGridLoadOnDemandRowStyle}"/>

Row Appearance after Setting the LoadOnDemandRowStyle

DataGrid LoadOnDemand Row Style

LoadOnDemandRowTemplate

The LoadOnDemandRowTemplate property can be used to set the template of the row that contains the Load More button when the LoadOnDemandMode is Manual.

The following example demonstrates a custom DataTemplate:

<DataTemplate x:Key="CustomLoadOnDemandRowTemplate">
    <Label Text="Load more from Template"
           Margin="0,30,0,30"
           HorizontalOptions="CenterAndExpand"
           VerticalOptions="CenterAndExpand"
           IsEnabled="{Binding IsDataLoading}">
        <Label.Triggers>
            <Trigger TargetType="Label"
                     Property="IsEnabled" Value="False">
                <Setter Property="BackgroundColor" Value="LightBlue" />
            </Trigger>
        </Label.Triggers>
    </Label>
</DataTemplate>

The following example shows how to set the property:

<telerik:RadDataGrid x:Name="dataGrid"
                     ItemsSource="{Binding Items}"
                     LoadOnDemand="dataGrid_LoadOnDemand"
                     LoadOnDemandMode="Manual"
                     LoadOnDemandRowTemplate="{StaticResource CustomLoadOnDemandRowTemplate}"/>

Row Appearance after Setting the LoadOnDemandRowTemplate

DataGrid LoadOnDemand Row Style

See Also

In this article
Not finding the help you need?