See Also: DataKey Members
The System.Web.UI.WebControls.DataKey class is used to represent the primary key for a record in a data-bound control. The primary key for a record can be composed of one of more fields from the data source. Although the System.Web.UI.WebControls.DataKey class is not a collection, it can store multiple key field values. The key field values are populated when one of the constructors for the System.Web.UI.WebControls.DataKey class is called. You can retrieve a key field value from a System.Web.UI.WebControls.DataKey object in the following ways:
Use the DataKey.Item(int) property to retrieve a key field value at a specific index in the System.Web.UI.WebControls.DataKey object.
Use the DataKey.Item(string) property to retrieve a key field value of a specific field.
Use the DataKey.Value property to retrieve the value of the key field at index 0 in the System.Web.UI.WebControls.DataKey object. This property is often used as a shortcut to retrieve the key value of a record when the primary key contains only one field.
Use the DataKey.Values property to create an System.Collections.Specialized.IOrderedDictionary object that can be used to iterate through the key field values.
In general, System.Web.UI.WebControls.DataKey objects are automatically generated by data-bound controls when the control's DataKeyNames property is set. The System.Web.UI.WebControls.DataKey objects contain the values of the key field or fields specified in the DataKeyNames property. Data-bound controls that display a single record at a time (such as System.Web.UI.WebControls.DetailsView or System.Web.UI.WebControls.FormView) generally store the System.Web.UI.WebControls.DataKey object for the current record displayed in the DataKey property of the control. Data-bound controls that display multiple records at a time (such as System.Web.UI.WebControls.GridView) generally store the System.Web.UI.WebControls.DataKey objects for each record in the control in a System.Web.UI.WebControls.DataKeyArray collection. The System.Web.UI.WebControls.DataKeyArray collection is then stored in the DataKeys property of the control.