DxGrid.SelectedDataItem Property
In single selection mode, this property specifies the data item that corresponds to the selected Grid row.
Namespace: DevExpress.Blazor
Assembly: DevExpress.Blazor.v24.2.dll
NuGet Package: DevExpress.Blazor
Declaration
[DefaultValue(null)]
[Parameter]
public object SelectedDataItem { get; set; }
Property Value
Type | Default | Description |
---|---|---|
Object | null | The data item. |
Remarks
Set the SelectionMode property to Single
to enable single row selection. To access the data item that corresponds to the selected row, use the SelectedDataItem
property as follows:
- Implement two-way binding for this property (
@bind-SelectedDataItem
) to specify the initially selected row and automatically update the property value when selection changes. - Use one-way binding for this property and handle the SelectedDataItemChanged event to implement a custom response to selection changes.
If SelectionMode is set to Multiple
(default value), use the SelectedDataItems property instead. If you change the selection mode dynamically, specify the SelectedDataItems property and handle the SelectedDataItemsChanged event. In the event handler, check the selection mode and assign the last selected item or all selected items to the SelectedDataItems property.
For more information about selection in the Grid component, refer to the following topic: Selection and Focus in Blazor Grid.
Select and Deselect Items
A row can be selected and deselected in the following ways:
- A user clicks a row, taps it, or executes keyboard shortcuts to select or deselect the row. To enable this functionality, set the AllowSelectRowByClick property to
true
. - A user clicks a radio button in the selection column. To display this column, declare a DxGridSelectionColumn object in the Columns template.
- Call the
Select*
orDeselect*
method. Refer to the Grid’s member table for a list of available methods.
Obtain Item Field Values
Pass the SelectedDataItem
property value to the GetDataItemValue method to get the item’s field value when the Grid is bound to one of the following data sources:
- An Instant Feedback Data Source whose AreSourceRowsThreadSafe option is set to
false
(its default value) - A collection of anonymous objects
In other cases, you can cast the SelectedDataItem
property value to the corresponding type and use the {DataItem.FieldName}
notation to get the item’s field value.
The following example allows users to click a row to select it and displays information about that row:
@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();
}
}