System.Web.UI.WebControls.GridView.DataKeyNames Property

Gets or sets an array that contains the names of the primary key fields for the items displayed in a System.Web.UI.WebControls.GridView control.

Syntax

[System.ComponentModel.Editor("System.Web.UI.Design.WebControls.DataFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.StringArrayConverter))]
[System.ComponentModel.DefaultValue(null)]
public virtual string[] DataKeyNames { set; get; }

Value

Documentation for this section has not yet been entered.

Remarks

Use the GridView.DataKeyNames property to specify the field or fields that represent the primary key of the data source. You should only set this property to the field or fields that are required to uniquely identify each row; for example, the ID column if an integer value uniquely identifies each row. You must set the GridView.DataKeyNames property in order for the automatic update and delete features of the System.Web.UI.WebControls.GridView control to work. The values of these key fields are passed to the data source control in order to specify the row to update or delete.

If you need to retrieve the data key value when updating or deleting a row, use the Keys property of either the System.Web.UI.WebControls.GridViewUpdateEventArgs or System.Web.UI.WebControls.GridViewDeleteEventArgs class. For example, e.Keys[0] holds the value of the first data key in a GridView.RowUpdating or GridView.RowDeleting event handler.

If you need to retrieve the data key value when a row is selected, use the GridView.SelectedDataKey property.

When the GridView.DataKeyNames property is set, the System.Web.UI.WebControls.GridView control automatically populates its GridView.DataKeys collection with the values from the specified field or fields, which provides a convenient way to access the primary keys of each row.

Note:

The System.Web.UI.WebControls.GridView control stores these key field values in the control state. If these values contain sensitive information, it is strongly recommended that you enable view-state encryption by setting the System.Web.UI.Page.ViewStateEncryptionMode property to ViewStateEncryptionMode.Always.

When you use automatically generated field columns (by setting the GridView.AutoGenerateColumns property to true), the System.Web.UI.WebControls.GridView control makes sure that the columns that correspond to the field or fields specified in the GridView.DataKeyNames property are read-only.

If the DataControlField.Visible property of a column field is set to false, the column is not displayed in the System.Web.UI.WebControls.GridView control and the data for the column does not make a round trip to the client. If you want the data for a column that is not visible to be available to the client, add the field name to the GridView.DataKeyNames property.

Requirements

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
Assembly Versions: 2.0.0.0
Since: .NET 2.0