Represents a collection that contains the key field of each record in a data source. This class cannot be inherited.
See Also: DataKeyCollection Members
The System.Web.UI.WebControls.DataKeyCollection class represents a collection of the key fields in the data source. The key field of each record in the data source is stored in this collection. This allows you to store the key field with a data listing control without displaying it in the control. This collection is automatically filled with the values from the field specified by the BaseDataList.DataKeyField property. This collection does not allow you to manually add or remove items from the collection.
The key field is commonly used in a handler for an event, such as ItemCommand or DeleteCommand, as part of an update query string to revise a specific record in the data source. The key field helps the update query string identify the appropriate record to modify.
Use the DataKeyCollection.Count property to determine the number of items in the collection. To programmatically retrieve a key field from a System.Web.UI.WebControls.DataKeyCollection, use one of following methods:
Use the indexer to get a single key field from the collection, using array notation.
Use the DataKeyCollection.CopyTo(Array, int) method to copy the contents of the collection to a Array object, which can then be used to get items from the collection.
Use the DataKeyCollection.GetEnumerator method to create a IEnumerator implemented object, which can then be used to get items from the collection.