Back to Devexpress

GridSettingsBase.KeyFieldName Property

aspnet-devexpress-dot-web-dot-mvc-dot-gridsettingsbase.md

latest7.1 KB
Original Source

GridSettingsBase.KeyFieldName Property

Gets or sets the name of the data source key field.

Namespace : DevExpress.Web.Mvc

Assembly : DevExpress.Web.Mvc5.v20.1.dll

NuGet Package : DevExpress.Web.Mvc5

Declaration

csharp
public string KeyFieldName { get; set; }
vb
Public Property KeyFieldName As String

Property Value

TypeDescription
String

A String value that specifies the name of the data source key field.

|

Remarks

The KeyFieldName property should be specified when using the following GridView extension features:

  • data editing;
  • adding new and deleting existing rows;
  • selecting rows;
  • master-detail view;
  • endless paging.

Note

  • The values contained in the key field must be unique.
  • It is not possible to use the edit form to update a key field value.

The GridSettingsBase property supports a composite KeyFieldName property value. You can use semicolon-separated values to specify multi-field keys, for example:

cshtml
@Html.DevExpress().GridView(
    settings => {
        settings.KeyFieldName = "id1;id2";
...
csharp
...
protected Action DeletePartial(int id1, int id2)
....

Example

razor
@Html.DevExpress().GridView(settings => {
    settings.Name = "gvDataBindingToLinq";
    settings.CallbackRouteValues = new { Controller = "DataBinding", Action = "DataBindingToLargeDatabasePartial" };
    settings.KeyFieldName = "ID";
    ...
}).BindToEF(string.Empty, string.Empty, (s, e) => {
    e.QueryableSource = LargeDatabaseDataProvider.DB.Emails;
}).GetHtml()

Online Demo

The following code snippets (auto-collected from DevExpress Examples) contain references to the KeyFieldName property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

asp-net-mvc-grid-export-multiple-gridviews-into-a-document/CS/Controllers/HomeController.cs#L28

csharp
categoriesGridSettings.Name = "gvCategories";
categoriesGridSettings.KeyFieldName = "CategoryID";
categoriesGridSettings.Columns.Add("CategoryID");

mvc-gridview-export-data-to-different-formats/CS/Controllers/HomeController.cs#L59

csharp
settings.KeyFieldName = "ID";
settings.Settings.ShowFilterRow = true;

asp-net-mvc-grid-export-hide-and-add-columns/CS/E4915/Controllers/HomeController.cs#L36

csharp
settings.KeyFieldName = "ProductID";
settings.Columns.Add("ProductID");

asp-net-mvc-grid-export-colored-grid-in-wysiwyg-mode/CS/Controllers/HomeController.cs#L47

csharp
settings.KeyFieldName = "ProductID";
settings.Settings.ShowFilterRow = true;

asp-net-mvc-grid-export-colored-grid-in-data-aware-mode/CS/Controllers/HomeController.cs#L57

csharp
settings.KeyFieldName = "ProductID";
settings.Settings.ShowFilterRow = true;

asp-net-mvc-grid-export-multiple-gridviews-into-a-document/VB/Controllers/HomeController.vb#L33

vb
categoriesGridSettings.Name = "gvCategories"
categoriesGridSettings.KeyFieldName = "CategoryID"
categoriesGridSettings.Columns.Add("CategoryID")

mvc-gridview-export-data-to-different-formats/VB/Controllers/HomeController.vb#L54

vb
settings.CallbackRouteValues = New With {.Controller = "Home", .Action = "TypedListDataBindingPartial"}
settings.KeyFieldName = "ID"
settings.Settings.ShowFilterRow = True

asp-net-mvc-grid-export-hide-and-add-columns/VB/E4915/Controllers/HomeController.vb#L37

vb
settings.KeyFieldName = "ProductID"

asp-net-mvc-grid-export-colored-grid-in-wysiwyg-mode/VB/Controllers/HomeController.vb#L54

vb
settings.KeyFieldName = "ProductID"
settings.Settings.ShowFilterRow = True

asp-net-mvc-grid-export-colored-grid-in-data-aware-mode/VB/Controllers/HomeController.vb#L66

vb
settings.KeyFieldName = "ProductID"
settings.Settings.ShowFilterRow = True

See Also

PreviewFieldName

Grid View

Knowledge Base Article: KA18689: Why may the "A primary key field specified via the KeyFieldName property is not found ..." exception occur

GridSettingsBase Class

GridSettingsBase Members

DevExpress.Web.Mvc Namespace