Back to Devexpress

GridSettingsBase.KeyFieldName Property

aspnetmvc-devexpress-dot-web-dot-mvc-dot-gridsettingsbase-96aeed21.md

latest7.5 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.v25.2.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.

Limitations

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");

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-selected-columns-in-BeforeExport-event-handler/CS/E3352/Controllers/HomeController.cs#L46

csharp
gridVieewSettings.Name = "gridView";
gridVieewSettings.KeyFieldName = "ID";
gridVieewSettings.CallbackRouteValues = new { Controller = "Home", Action = "GridViewPartial" };

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

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

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-multiple-gridviews-into-a-document/VB/Controllers/HomeController.vb#L33

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

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

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#L31

vb
settings.CallbackRouteValues = New With {.Controller = "Home", .Action = "GridViewPartial"}
settings.KeyFieldName = "ProductID"
settings.Columns.Add("ProductID")

See Also

PreviewFieldName

Grid View

Troubleshooting: 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