DxDataGrid<T>.KeyFieldName Property

Specifies the key data field’s name.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v21.1.dll

Declaration

[Parameter]
public string KeyFieldName { get; set; }

Property Value

Type Description
String

The key field’s name.

Remarks

The Data Grid should be able to identify individual data items when users edit data, select data rows, or expand detail rows in master-detail layouts.

You can specify the key data field in one of the following ways:

  • Define the Data Grid’s KeyFieldName property explicitly.

    @using System.Threading
    @inject WeatherForecastService WeatherForecastService
    
    <DxDataGrid DataAsync="@LoadDataAsync" KeyFieldName="Id">
        @*...*@
    </DxDataGrid>
    
    @code {
        async Task<IEnumerable<WeatherForecast>> LoadDataAsync(CancellationToken token) {
            return await WeatherForecastService.GetForecastAsync(DateTime.Now.Date);
        }
    }
    
  • Mark the key data field with the Key attribute in a data collection. Note the KeyFieldName property takes priority over this attribute.

    using System;
    using System.ComponentModel.DataAnnotations;
    
    public class WeatherForecast {
        [Key]
        public int Id { get; set; }
        public DateTime Date { get; set; }
        public int TemperatureC { get; set; }
        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
        public string Summary { get; set; }
    }
    
  • Use the CustomData property and the PrimaryKey array to specify multiple key fields:

    <DxDataGrid T="@WebApiOrder" CustomData="@LoadCustomData">
        <!-- ... -->
    </DxDataGrid>
    @code {
        IQueryable<WebApiOrder> _data;
        protected Task<LoadResult> LoadCustomData(DataSourceLoadOptionsBase options, CancellationToken cancellationToken)
        {
            // ...
            options.PrimaryKey = new[] { "Key1", "Key2" };
            return DataSourceLoader.LoadAsync(_data, options, cancellationToken);
        }
    }
    
NOTE

The key field’s values should be unique.

If you do not specify the key field name, the standard Equals method is used to compare data items.

Run Demo: Data Grid - Multiple Selection with Optimized Performance

See Also