aspnet-devexpress-dot-web-dot-aspxgridbase.md
Gets or sets the name of the data source key field.
Namespace : DevExpress.Web
Assembly : DevExpress.Web.v25.2.dll
NuGet Package : DevExpress.Web
[DefaultValue("")]
public string KeyFieldName { get; set; }
<DefaultValue("")>
Public Property KeyFieldName As String
| Type | Default | Description |
|---|---|---|
| String | String.Empty |
A String value that specifies the name of the data source key field.
|
The KeyFieldName property should be specified when using the following grid features:
The KeyFieldName property supports a composite value. You can use semicolon-separated values to specify multi-field keys, for example:
<dx:ASPxGridView ... KeyFieldName="FirstName;LastName;Phone;BirthDate">
...
</dx:ASPxGridView>
The following code snippet illustrates how to select a row when a composite key is used.
ASPxGridView1.Selection.SelectRowByKey("Key1|Key2|...|KeyN");
Note
For a full example, see the ASPxGridView - LargeDatabase (Server Mode) demo.
<dx:ASPxGridView ID="grid" ClientInstanceName="grid" runat="server" KeyFieldName="ID"
Width="100%" AutoGenerateColumns="False">
<Columns>
<dx:GridViewDataTextColumn FieldName="From" Width="200px" />
<dx:GridViewDataTextColumn FieldName="Subject" />
<dx:GridViewDataDateColumn FieldName="Sent" Width="100px" />
<dx:GridViewDataCheckColumn Caption="Attachment?" FieldName="HasAttachment" Width="100px" />
<dx:GridViewDataTextColumn FieldName="Size" Width="80px">
<Settings AllowAutoFilter="false" />
</dx:GridViewDataTextColumn>
</Columns>
...
</dx:ASPxGridView>
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.
protected void grid_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {
int productID = (int)e.Keys[grid.KeyFieldName];
Product product = DataContext.Products.Where(p => p.ProductID == productID).FirstOrDefault();
asp-net-web-forms-grid-create-master-detail-grid-at-runtime/CS/MasterDetailGrids/Default.aspx.cs#L34
masterGrid.SettingsDetail.ShowDetailRow = true;
masterGrid.KeyFieldName = "CategoryID";
masterGrid.DataSource = GetMasterDataSource();
aspxgridview-edit-in-memory-dataset/CS/WebApp/Default.aspx.cs#L74
protected void MasterGridView_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {
int i = MasterGridView.FindVisibleIndexByKeyValue(e.Keys[MasterGridView.KeyFieldName]);
Control c = MasterGridView.FindDetailRowTemplateControl(i, "ASPxGridView2");
asp-net-web-forms-grid-programmatically-bind-combobox-column/CS/Solution/Default.aspx.cs#L19
ASPxGridView1.DataSource = Bug.GetBugList();
ASPxGridView1.KeyFieldName = "Id";
asp-net-web-forms-grid-create-at-runtime/CS/Default.aspx.cs#L19
grid.ID = "grid";
grid.KeyFieldName = "ID";
grid.DataSource = GetData();
Protected Sub grid_RowDeleting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataDeletingEventArgs)
Dim productID As Integer = CInt(e.Keys(grid.KeyFieldName))
Dim product As Product = DataContext.Products.Where(Function(p) p.ProductID = productID).FirstOrDefault()
asp-net-web-forms-grid-create-master-detail-grid-at-runtime/VB/MasterDetailGrids/Default.aspx.vb#L36
masterGrid.SettingsDetail.ShowDetailRow = True
masterGrid.KeyFieldName = "CategoryID"
masterGrid.DataSource = GetMasterDataSource()
aspxgridview-edit-in-memory-dataset/VB/WebApp/Default.aspx.vb#L86
Protected Sub MasterGridView_RowDeleting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataDeletingEventArgs)
Dim i As Integer = MasterGridView.FindVisibleIndexByKeyValue(e.Keys(MasterGridView.KeyFieldName))
Dim c As Control = MasterGridView.FindDetailRowTemplateControl(i, "ASPxGridView2")
asp-net-web-forms-grid-programmatically-bind-combobox-column/VB/Solution/Default.aspx.vb#L18
ASPxGridView1.DataSource = Bug.GetBugList()
ASPxGridView1.KeyFieldName = "Id"
statuses = BugStatus.GetStatusesList()
asp-net-web-forms-grid-create-at-runtime/VB/Default.aspx.vb#L17
grid.ID = "grid"
grid.KeyFieldName = "ID"
grid.DataSource = GetData()
See Also
Troubleshooting: A primary key field specified using the KeyFieldName property is not found