DxGrid.SelectDataItem(Object, Boolean) Method
Selects or deselects a row that corresponds to the specified data item.
Namespace: DevExpress.Blazor
Assembly: DevExpress.Blazor.v24.2.dll
NuGet Package: DevExpress.Blazor
Declaration
public void SelectDataItem(
object dataItem,
bool selected = true
)
Parameters
Name | Type | Description |
---|---|---|
dataItem | Object | The data item. |
Optional Parameters
Name | Type | Default | Description |
---|---|---|---|
selected | Boolean | True |
|
Remarks
The following methods allow you to manage Grid selection:
- SelectRow (DeselectRow)
- SelectRows (DeselectRows)
SelectDataItem
(DeselectDataItem)- SelectDataItems (DeselectDataItems)
- SelectAllOnPage (DeselectAllOnPage)
- SelectAllAsync (DeselectAllAsync)
- ClearSelection
Call the SelectDataItem
method and pass a data item as the parameter to select the corresponding row. The method’s behavior depends on the SelectionMode property value:
In multiple selection mode, the method keeps the previously selected rows and adds the specified row to the selection. To access selected data items, implement two-way binding for the SelectedDataItems property or handle the SelectedDataItemsChanged event.
In single selection mode, the method changes selection to the specified row. To access the selected data item, implement two-way binding for the SelectedDataItem property or handle the SelectedDataItemChanged event.
To deselect a row and its corresponding data item, do any of the following:
- Call the DeselectDataItem method.
- Call the
SelectDataItem
method and passfalse
as the second parameter.
The following example selects and deselects a row that corresponds to the first data item:
@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.SelectDataItem(GridDataSource.First())">Select First Data Item</DxButton>
<DxButton Click="() => MyGrid.DeselectDataItem(GridDataSource.First())">Deselect First Data Item</DxButton>
@code {
IEnumerable<object> GridDataSource { get; set; }
NorthwindContext Northwind { get; set; }
IReadOnlyList<object> SelectedDataItems { get; set; }
IGrid MyGrid { 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();
}
}
For more information about selection in the Grid component, refer to the following topic: Selection and Focus in Blazor Grid.