Skip to main content
All docs
V26.1
  • DxGrid.KeyFieldName Property

    Specifies the name of a data field that contains unique identifiers for Grid data items.

    Namespace: DevExpress.Blazor

    Assembly: DevExpress.Blazor.Grid.v26.1.dll

    NuGet Package: DevExpress.Blazor

    Declaration

    [DefaultValue(null)]
    [Parameter]
    public string KeyFieldName { get; set; }

    Property Value

    Type Default Description
    String null

    The key field’s name.

    Remarks

    The KeyFieldName property specifies the key field name for data objects. If your data objects have 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 KeyFieldName (or KeyFieldNames) 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 following code snippet enables row selection by mouse clicks and specifies the ProductId field as a key.

    @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();
        }
    }
    

    Blazor Grid Multiple Row Selection

    Run Demo: Data Grid - Multiple Row Selection

    Implements

    See Also