Note: This property is new in the .NET Framework version 2.0.
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)
Visual Basic (Declaration)
Public Overridable ReadOnly Property SelectedDataKey As DataKey
Dim instance As GridView
Dim value As DataKey
value = instance.SelectedDataKey
public virtual DataKey SelectedDataKey { get; }
public:
virtual property DataKey^ SelectedDataKey {
DataKey^ get ();
}
/** @property */
public DataKey get_SelectedDataKey ()
public function get SelectedDataKey () : DataKey
Property Value
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.
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.
The following code example demonstrates how to use the SelectedDataKey property to determine the data key value of the selected row in a GridView control.
<%@ Page language="VB" %>
<script runat="server">
Sub CustomersGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
' Display the primary key value of the selected row.
Message.Text = "The primary key value of the selected row is " & _
CustomersGridView.SelectedDataKey.Value.ToString() & "."
End Sub
</script>
<html>
<body>
<form runat="server">
<h3>GridView SelectedDataKey Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/><br/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
allowpaging="true"
autogeneratecolumns="true"
autogenerateselectbutton="true"
datakeynames="CustomerID"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
runat="server">
<selectedrowstyle backcolor="LightBlue"
forecolor="DarkBlue"/>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
<%@ Page language="C#" %>
<script runat="server">
void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Display the primary key value of the selected row.
Message.Text = "The primary key value of the selected row is " +
CustomersGridView.SelectedDataKey.Value.ToString() + ".";
}
</script>
<html>
<body>
<form runat="server">
<h3>GridView SelectedDataKey Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/><br/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
allowpaging="true"
autogeneratecolumns="true"
autogenerateselectbutton="true"
datakeynames="CustomerID"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
runat="server">
<selectedrowstyle backcolor="LightBlue"
forecolor="DarkBlue"/>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
The following code 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.
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>Selecting Data Key Values</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * FROM [Order Details]">
</asp:SqlDataSource>
<asp:SqlDataSource
ID="SqlDataSource2"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * FROM [Products]WHERE [ProductID] = @productid">
<SelectParameters>
<asp:ControlParameter
Name="productid"
ControlID="GridView1"
PropertyName="SelectedDataKey[1]" />
</SelectParameters>
</asp:SqlDataSource>
</div>
<asp:GridView
ID="GridView1"
runat="server"
AllowPaging="True"
AutoGenerateColumns="False"
DataKeyNames="OrderID,ProductID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True"/>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
<asp:BoundField DataField="Discount" HeaderText="Discount" />
</Columns>
</asp:GridView>
<br />
<asp:DetailsView
ID="DetailsView1"
runat="server"
AutoGenerateRows="False"
DataKeyNames="ProductID"
DataSourceID="SqlDataSource2"
Height="50px" Width="125px">
<Fields>
<asp:BoundField
DataField="ProductID"
HeaderText="ProductID"
InsertVisible="False"
ReadOnly="True" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName"/>
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID"/>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" />
<asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" />
<asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" />
<asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" />
</Fields>
</asp:DetailsView>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>Selecting Data Key Values</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * FROM [Order Details]">
</asp:SqlDataSource>
<asp:SqlDataSource
ID="SqlDataSource2"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * FROM [Products]WHERE [ProductID] = @productid">
<SelectParameters>
<asp:ControlParameter
Name="productid"
ControlID="GridView1"
PropertyName="SelectedDataKey[1]" />
</SelectParameters>
</asp:SqlDataSource>
</div>
<asp:GridView
ID="GridView1"
runat="server"
AllowPaging="True"
AutoGenerateColumns="False"
DataKeyNames="OrderID,ProductID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True"/>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
<asp:BoundField DataField="Discount" HeaderText="Discount" />
</Columns>
</asp:GridView>
<br />
<asp:DetailsView
ID="DetailsView1"
runat="server"
AutoGenerateRows="False"
DataKeyNames="ProductID"
DataSourceID="SqlDataSource2"
Height="50px" Width="125px">
<Fields>
<asp:BoundField
DataField="ProductID"
HeaderText="ProductID"
InsertVisible="False"
ReadOnly="True" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName"/>
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID"/>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" />
<asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" />
<asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" />
<asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" />
</Fields>
</asp:DetailsView>
</form>
</body>
</html>
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
.NET Framework
Supported in: 2.0