Skip to main content

GridCustomDataSource.GetItemCountAsync(GridCustomDataSourceCountOptions, CancellationToken) Method

Returns the number of data source items that match specified options.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.Grid.v26.1.dll

NuGet Package: DevExpress.Blazor

Declaration

public abstract Task<int> GetItemCountAsync(
    GridCustomDataSourceCountOptions options,
    CancellationToken cancellationToken
)

Parameters

Name Type Description
options GridCustomDataSourceCountOptions

An object that contains options that data source items should match.

cancellationToken CancellationToken

An object that propagates a cancellation notification.

Returns

Type Description
Task<Int32>

An asynchronous operation that returns the number of items.

Remarks

If standard data binding modes do not meet specific business requirements, you can implement your own data provider. To do so, create a GridCustomDataSource descendant and assign it to the Grid or TreeList component’s Data property.

The following code snippet implements a custom data provider’s GetItemCountAsync method:

View Example: Custom Data Source Binding using Entity Framework Core

using DevExpress.Blazor;
using DevExpress.Data.Filtering;
using DevExpress.Data.Filtering.Helpers;
using CustomDataSource.Models;
using System.Collections;
using System.Linq.Expressions;
using DevExpress.Data.Linq;
using DevExpress.Data.Linq.Helpers;
using Microsoft.EntityFrameworkCore;

namespace CustomDataSource.Services;

public class OrdersDataSource : GridCustomDataSource, IDisposable {
    protected override Type DataItemType => typeof(Order);
    private readonly NorthwindContext _context;
    private bool _disposed = false;

    public OrdersDataSource(IDbContextFactory<NorthwindContext> contextFactory) {
        _context = contextFactory.CreateDbContext();
    } 

    public override Task<int> GetItemCountAsync(GridCustomDataSourceCountOptions options, CancellationToken cancellationToken) {
        return ApplyFiltering(options.FilterCriteria, _context.Orders)
            .CountAsync(cancellationToken);
    }
    // ...
    private static IQueryable<Order> ApplyFiltering(CriteriaOperator criteria, IQueryable<Order> queryableSource) {
        return !criteria.ReferenceEqualsNull()
            ? (IQueryable<Order>)queryableSource.AppendWhere(
                new CriteriaToEFExpressionConverter(queryableSource.Provider.GetType()), criteria)
            : queryableSource;
    }
    // ...
    public void Dispose() {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing) {
        if (!_disposed) {
            if (disposing) {
                _context?.Dispose();
            }
            _disposed = true;
        }
    }
    // ...
}
See Also