Skip to main content

DxGrid.SelectDataItems(IEnumerable<Object>, Boolean) Method

Selects or deselects rows that correspond to the specified data items.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v24.1.dll

NuGet Package: DevExpress.Blazor

Declaration

public void SelectDataItems(
    IEnumerable<object> dataItems,
    bool selected = true
)

Parameters

Name Type Description
dataItems IEnumerable<Object>

A collection of data items.

Optional Parameters

Name Type Default Description
selected Boolean True

true to select rows; false to deselect rows.

Remarks

The following methods allow you to manage Grid selection:

The SelectDataItems method allows you to add multiple rows to the current selection when the SelectionMode property is set to GridSelectionMode.Multiple. Call this method and pass a collection of data items that correspond to these rows.

If you call the SelectDataItems method in GridSelectionMode.Single mode, it clears selection and selects only the row that corresponds to the last specified data item.

To deselect rows and their corresponding data items, do any of the following:

  • Call the DeselectDataItems method.
  • Call the SelectDataItems method and pass false as the second parameter.

To access the selected data items, implement two-way binding for the SelectedDataItems property or handle the SelectedDataItemsChanged event.

@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<NorthwindContext> NorthwindContextFactory
@implements IDisposable

<DxGrid Data="GridDataSource"
        AllowSelectRowByClick="true"
        @bind-SelectedDataItems="@SelectedDataItems"
        KeyFieldName="ProductId"
        @ref="MyGrid">
    <Columns>
        <DxGridDataColumn FieldName="ProductName" />
        <DxGridDataColumn FieldName="UnitPrice" />
        <DxGridDataColumn FieldName="QuantityPerUnit" />
        <DxGridDataColumn FieldName="UnitsInStock" />
    </Columns>
</DxGrid>
<br />

<DxButton Click="() => MyGrid.SelectDataItems(dataItems)">Select Data Items 4-6</DxButton>
<DxButton Click="() => MyGrid.DeselectDataItems(dataItems)">Deselect Data Items 4-6</DxButton>

@code {
    IEnumerable<object> GridDataSource { get; set; }
    NorthwindContext Northwind { get; set; }
    IReadOnlyList<object> SelectedDataItems { get; set; }
    IGrid MyGrid { get; set; }
    IEnumerable<object> dataItems { get; set; }

    protected override void OnInitialized() {
        Northwind = NorthwindContextFactory.CreateDbContext();
        GridDataSource = Northwind.Products
            .ToList();
        SelectedDataItems = GridDataSource.Skip(1).Take(2).ToList();
        dataItems = GridDataSource.Skip(3).Take(3).ToList();
    }

    public void Dispose() {
        Northwind?.Dispose();
    }
}

Blazor Grid Select and Deselect Data Items

Run Demo: Data Grid - Selection API

For more information about selection in the Grid component, refer to the following topic: Selection and Focus in Blazor Grid.

See Also