System.Web.UI.WebControls Namespace GridView Class
.NET Framework version 2.0
Returns a collection of DataKey objects that represent the data key value of each row in a GridView control.
Script |
[ DataKeyArray var = ] GridView.DataKeys |
This property can only be used programmatically; it cannot be set when declaring the control.
A DataKeyArray object that represents the data key for each row in a GridView control.
Use the DataKeys collection to access the key values of each record in a GridView control.
NOTE: The DataKeys collection will only have values when the DataKeyNames property is set.
When the DataKeyNames property is set, the GridView control automatically populates its DataKeys collection with the values from the specified field or fields, which provides a convenient way to store and retrieve the primary keys of each row, without having to display it in the control.
The GridView provides several properties for retrieving the selected item in the DataKeys collection.
- the SelectedValue property retrieves the data key value for the currently selected row directly.
- the SelectedDataKey property retrieves the DataKey object for the currently selected row.
- the SelectedIndex property retrieves the selected row index which can be used to retrieve the DataKey object at that index.
The key field is commonly used in a handler for an event, such as the SelectedIndexChanged event, where the value of the key field is used to build the SQL Select or Update query to retrieve and/or revise a specific record in the data source. The key field simplifies identifying the appropriate record to display or modify.
The following examples demonstrate using the properties described above to retrieve the selected item in the DataKeys collection.
GridView Members DataKeyNames