Skip to main content
All docs
V24.1

DxListBox<TData, TValue>.KeyFieldName Property

Specifies the key field used for data item identification when the component uses a business object as a value.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v24.1.dll

NuGet Package: DevExpress.Blazor

Declaration

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

Property Value

Type Default Description
String String.Empty

The key field’s name.

Remarks

Note

The KeyFieldName property was introduced in v24.1.6.

When the List Box works with business objects, we recommend that you set the KeyFieldName property. If your data objects have a compound key, use the KeyFieldNames property instead. The component uses specified key fields to identify and compare data items.

Set the KeyFieldName/KeyFieldNames property in the following cases:

  • When you use the CustomData property to bind the List Box to data stored on a remote service and loaded through a Web API.
  • When you use the Data property to bind the List Box to a data collection (Data) that stores business objects (IEnumerable<CustomType>) and you specify a business object in the Value property.

If you do not specify key fields, the List Box uses standard .NET value equality comparison to identify and compare data items. In this case, you should override object type’s Equals and GetHashCode methods to ensure correct identification of objects. However, the best way to identify objects is to use KeyFieldName and KeyFieldNames properties.

The following code specifies the ProductId field as a key:

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

<DxListBox Data="DataSource"
           @bind-Values="@Values"
           KeyFieldName="ProductId">
    <Columns>
        <DxListEditorColumn FieldName="ProductName" />
        <DxListEditorColumn FieldName="UnitPrice" />
        <DxListEditorColumn FieldName="QuantityPerUnit" />
        <DxListEditorColumn FieldName="UnitsInStock" />
    </Columns>
</DxListBox>

@code {
    IEnumerable<object> DataSource { get; set; }
    NorthwindContext Northwind { get; set; }
    IEnumerable<Order> Values { get; set; }

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

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

Implements

DevExpress.Blazor.IListEditorBase<TData, TValue>.KeyFieldName
See Also