Export (0) Print
Expand All
Expand Minimize

GridView.SelectedDataKey Property

Gets the DataKey object that contains the data key value for the selected row in a GridView control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

[<BrowsableAttribute(false)>]
abstract SelectedDataKey : DataKey with get
[<BrowsableAttribute(false)>]
override SelectedDataKey : DataKey with get

Property Value

Type: System.Web.UI.WebControls.DataKey
The DataKey for the selected row in a GridView control. The default is a null reference (Nothing in Visual Basic), which indicates that no row is currently selected.

ExceptionCondition
InvalidOperationException

No data keys are specified in the DataKeyNames property.

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.

NoteNote

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.

The following example demonstrates how to use the SelectedDataKey property to determine the data key value of the selected row in a GridView control.

No code example is currently available or this language may not be supported.

The following example demonstrates how to use the second key field as a parameter in a master/detail scenario. A GridView control is used to display records from the Order Details table of the Northwind database. When a record is selected in the GridView control, the details of the product from the Products table are displayed in a DetailsView control. ProductID is the second key name in the GridView control. To access the second key, the value of GridView1.SelectedDataKey[1] is used as the PropertyName for the ControlParameter object of the SqlDataSource control of the DetailsView control.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft