System.Web.UI.WebControls Namespace GridView Class
.NET Framework version 2.0
Returns the data key value of the selected row in a GridView control.
Script |
GridView.SelectedValue [ = objDataKeyvalue ] |
This property can only be used programmatically; it cannot be set when declaring the control.
objDataKeyvalue |
An object specifying the data key value of the selected row in a GridView control. |
The property is read only with no default value.
Use the SelectedValue property to determine the value of the row selected by the user in the GridView control.
The following example demonstrates how to use the SelectedValue property to determine the data key value of the selected row in a GridView control.
In this example, the GridView's SelectedValue property is defined as a parameter for the SelectCommand of a SqlDataSource control bound to a FormView control. This value is used by the data source control provider to specify which row to fetch in the data source and bind to the FormView.
<asp:sqldatasource id = "bookDetails" runat = "server"
datasourcemode = "datareader"
selectcommand = "SELECT * FROM pubs_Titles WHERE title_id = @title_id"
connectionstring = "<%$ ConnectionStrings:aspnet %>">
<selectparameters>
<asp:controlparameter controlid = "gvTitles"
name = "title_id" type = "string"
propertyname = "SelectedValue" />
</selectparameters>
</asp:sqldatasource>
SelectedDataKey SelectedRow Allowing Users to Select Rows in a GridView Control