Skip to main content
A newer version of this page is available. .

DxGrid.KeyFieldNames Property

Specifies the names of data fields that contain key values. The combination of key values forms a unique identifier for a Grid data item.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v23.1.dll

NuGet Package: DevExpress.Blazor

Declaration

[Parameter]
public IReadOnlyList<string> KeyFieldNames { get; set; }

Property Value

Type Description
IReadOnlyList<String>

The list of key field names.

Remarks

In common cases, a data object has a single key (a primary key). Use the KeyFieldName property to specify the key field name. If your data object has a compound key, use the KeyFieldNames property to specify key field names.

The Grid uses this field’s values to identify and compare data items. You should specify the KeyFieldNames (or KeyFieldName) property if the Grid in your application allows to perform the following actions:

If you do not specify key fields, the Grid uses standard .NET value equality comparison.

The code below specifies the OrderId and ProductId field values as keys for the OrderDetail data objects and enables row selection.

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

<DxGrid Data="GridDataSource"
        AllowSelectRowByClick="true"
        @bind-SelectedDataItems="@SelectedDataItems"
        KeyFieldNames="KeyFieldNames">
    <Columns>
        <DxGridDataColumn FieldName="OrderId" Caption="Order ID" DisplayFormat="d"/>
        <DxGridDataColumn FieldName="ProductId" Caption="Product ID" />
        <DxGridDataColumn FieldName="UnitPrice" DisplayFormat="c2" />
        <DxGridDataColumn FieldName="Quantity" />
        <DxGridDataColumn FieldName="Discount" />
    </Columns>
</DxGrid>

<br />
<div>
    <b>Selected orders:</b>
    @foreach (var order in SelectedDataItems.Cast<OrderDetail>()) {
        <li>Order ID: @order.OrderId, Product ID: @order.ProductId</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.OrderDetails
            .ToList();
        SelectedDataItems = GridDataSource.Skip(1).Take(2).ToList();
    }

    IReadOnlyList<string> KeyFieldNames = new [] {
        "OrderId",
        "ProductId",
    };

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

Key Field Names

Implements

See Also