When the DataKeyNames property is set, the GridView control automatically creates a DataKey object for each row in the control using the value or values of the specified field or fields. The DataKey objects are then added to the control's DataKeys collection. Normally, the DataKeys property is used to retrieve the DataKey object for a specific data row in the GridView control. However, if you just need to retrieve the DataKey object of the currently selected row, you can simply use the SelectedDataKey property as a shortcut.
Note |
|---|
| This is the same as retrieving the DataKey object at the index specified by the SelectedIndex property from the DataKeys collection. You can also use the SelectedValue property to retrieve the data key value for the currently selected row directly. |
If you are creating a ControlParameter object and want to access a key field other than the first field, use the indexed SelectedDataKey property in the PropertyName property of the ControlParameter object. An example is shown below.