Skip to main content

DxGrid.AllowSelectRowByClick Property

Specifies whether users can select and deselect rows by mouse clicks.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v23.2.dll

NuGet Package: DevExpress.Blazor

Declaration

[DefaultValue(false)]
[Parameter]
public bool AllowSelectRowByClick { get; set; }

Property Value

Type Default Description
Boolean false

true to allow users to select and deselect rows by mouse clicks; otherwise, false.

Remarks

Set the AllowSelectRowByClick property to true to enable row selection by mouse clicks.

<DxGrid Data="@DataSource"
        AllowSelectRowByClick="true"
        ...>
    @*...*@
</DxGrid>

The Grid compares and identifies data items to ensure correct selection operations. If your data object has a primary key, assign it to the KeyFieldName or KeyFieldNames property. Otherwise, the Grid uses standard .NET value equality comparison to identify data items.

The Grid supports multiple and single row selection.

Multiple Row Selection

The Grid allows users to select multiple rows simultaneously if the SelectionMode property is set to GridSelectionMode.Multiple (the default value). To select a range of rows, a user should click the first row in the range, hold down the Shift key, and click the last row in the range. To add a row to or remove a row from a selection, a user should hold down the Ctrl key and click that row. On touch devices, use long press gestures to select multiple rows.

Implement two-way binding for the SelectedDataItems property to specify and access data items that correspond to selected rows.

Blazor Grid Multiple Row Selection

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

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

<br/>
<div>
    <b>Selected products:</b>
    @foreach (var product in SelectedDataItems.Cast<Product>()) {
        <li>@product.ProductName</li>
    }
</div>

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

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

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

Run Demo: Data Grid - Multiple Row Selection

Single Row Selection

Set the SelectionMode property to GridSelectionMode.Single to allow users to select only one row at a time.

Implement two-way binding for the SelectedDataItem property to specify and access the data item that corresponds to the selected row.

Blazor Grid Single Row Selection

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

<DxGrid Data="GridDataSource"
        AllowSelectRowByClick="true"
        SelectionMode="GridSelectionMode.Single"
        @bind-SelectedDataItem="@SelectedDataItem"
        KeyFieldName="ProductId">
    <Columns>
        <DxGridDataColumn FieldName="ProductName" />
        <DxGridDataColumn FieldName="UnitPrice" />
        <DxGridDataColumn FieldName="QuantityPerUnit" />
        <DxGridDataColumn FieldName="UnitsInStock" />
    </Columns>
</DxGrid>

<br />
<div>
    <p><b>Selected product:</b> @((SelectedDataItem as Product)?.ProductName ?? "(none)")</p> 
</div>

@code {
    IEnumerable<object> GridDataSource { get; set; }
    NorthwindContext Northwind { get; set; }
    object SelectedDataItem { get; set; }

    protected override void OnInitialized() {
        Northwind = NorthwindContextFactory.CreateDbContext();
        GridDataSource = Northwind.Products
            .ToList();
        SelectedDataItem = GridDataSource.FirstOrDefault();
    }

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

Selection Column

You can also display a column that allows users to select and deselect rows. To do this, declare a DxGridSelectionColumn object in the Columns template. This column contains checkboxes in multiple selection mode and radio buttons in single selection mode.

See the DxGridSelectionColumn class description for more information.

Run Demo: Data Grid - Selection Column

Selection In Code

You can call the Select* and Deselect* methods to add rows to or remove rows from a selection. Refer to the Grid’s member table for the list of available methods.

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