Back to Devexpress

ASPxGridBase.KeyFieldName Property

aspnet-devexpress-dot-web-dot-aspxgridbase.md

latest8.6 KB
Original Source

ASPxGridBase.KeyFieldName Property

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

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
[DefaultValue("")]
public string KeyFieldName { get; set; }
vb
<DefaultValue("")>
Public Property KeyFieldName As String

Property Value

TypeDefaultDescription
StringString.Empty

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

|

Remarks

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

  • data editing;
  • adding new and deleting an existing data item (row, card or record);
  • selecting a data item (row, card or record);
  • master-detail view (for ASPxGridView);
  • endless paging.

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

aspx
<dx:ASPxGridView ... KeyFieldName="FirstName;LastName;Phone;BirthDate">
     ...
</dx:ASPxGridView>

Limitations

  • The values contained in the key field must be unique.
  • It is not recommended that you use the DateTime column names in the KeyFieldName property value.
  • Negative key values are not supported in Batch Edit Mode. Instead, use a composite value.
  • A key field cannot contain values of a reference type (for instance, arrays).
  • Complex type key values are not allowed. Refer to the following topic for more information: The control cannot serialize complex primary key field values.

The following code snippet illustrates how to select a row when a composite key is used.

csharp
ASPxGridView1.Selection.SelectRowByKey("Key1|Key2|...|KeyN");

Example

Note

For a full example, see the ASPxGridView - LargeDatabase (Server Mode) demo.

aspx
<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.

asp-net-web-forms-grid-entitydatasource-and-common-scenarios/CS/GridEntityFramework/CustomEditingCapabilities.aspx.cs#L28

csharp
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

csharp
masterGrid.SettingsDetail.ShowDetailRow = true;
masterGrid.KeyFieldName = "CategoryID";
masterGrid.DataSource = GetMasterDataSource();

aspxgridview-edit-in-memory-dataset/CS/WebApp/Default.aspx.cs#L74

csharp
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

csharp
ASPxGridView1.DataSource = Bug.GetBugList();
ASPxGridView1.KeyFieldName = "Id";

asp-net-web-forms-grid-create-at-runtime/CS/Default.aspx.cs#L19

csharp
grid.ID = "grid";
grid.KeyFieldName = "ID";
grid.DataSource = GetData();

asp-net-web-forms-grid-entitydatasource-and-common-scenarios/VB/GridEntityFramework/CustomEditingCapabilities.aspx.vb#L28

vb
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

vb
masterGrid.SettingsDetail.ShowDetailRow = True
masterGrid.KeyFieldName = "CategoryID"
masterGrid.DataSource = GetMasterDataSource()

aspxgridview-edit-in-memory-dataset/VB/WebApp/Default.aspx.vb#L86

vb
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

vb
ASPxGridView1.DataSource = Bug.GetBugList()
ASPxGridView1.KeyFieldName = "Id"
statuses = BugStatus.GetStatusesList()

asp-net-web-forms-grid-create-at-runtime/VB/Default.aspx.vb#L17

vb
grid.ID = "grid"
grid.KeyFieldName = "ID"
grid.DataSource = GetData()

See Also

Troubleshooting: A primary key field specified using the KeyFieldName property is not found

ASPxGridBase Class

ASPxGridBase Members

DevExpress.Web Namespace