Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
GridView Class
 SelectedValue Property
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
GridView..::.SelectedValue Property

Updated: November 2007

Gets the data key value of the selected row in a GridView control.

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

Visual Basic (Declaration)
<BrowsableAttribute(False)> _
Public ReadOnly Property SelectedValue As Object
Visual Basic (Usage)
Dim instance As GridView
Dim value As Object

value = instance.SelectedValue
C#
[BrowsableAttribute(false)]
public Object SelectedValue { get; }
Visual C++
[BrowsableAttribute(false)]
public:
property Object^ SelectedValue {
    Object^ get ();
}
J#
/** @property */
/** @attribute BrowsableAttribute(false) */
public Object get_SelectedValue()
JScript
public function get SelectedValue () : Object

Property Value

Type: System..::.Object

The data key value of the selected row in a GridView control.

When the DataKeyNames property is set with a comma-separated list of field names that represent the primary key of the data source, 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. As a further shortcut, you can directly determine the data key value of the first key field of the selected row by using the SelectedValue property.

If you are creating a ControlParameter object and want to access a key field other than the first field, use the SelectedDataKey property. For an example, see SelectedDataKey.

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

Visual Basic
<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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.SelectedValue.ToString() & "."

  End Sub

</script>

<html  >
  <head runat="server">
    <title>GridView SelectedValue Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView SelectedValue 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>


C#
<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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.SelectedValue.ToString() + ".";

  }

</script>

<html  >
  <head runat="server">
    <title>GridView SelectedValue Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView SelectedValue 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>


Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker