GridView.SelectedValue GridView.SelectedValue GridView.SelectedValue GridView.SelectedValue Property

Definición

Obtiene el valor de la clave de datos de la fila seleccionada en un control GridView.Gets the data key value of the selected row in a GridView control.

public:
 property System::Object ^ SelectedValue { System::Object ^ get(); };
[System.ComponentModel.Browsable(false)]
public object SelectedValue { get; }
member this.SelectedValue : obj
Public ReadOnly Property SelectedValue As Object

Valor de propiedad

Valor de la clave de datos de la fila seleccionada en un control GridView.The data key value of the selected row in a GridView control.

Ejemplos

En el ejemplo siguiente se muestra cómo utilizar SelectedValue la propiedad para determinar el valor de la clave de datos de la GridView fila seleccionada en un 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.


<%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
  <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>


<%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
  <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>

Comentarios

Cuando la DataKeyNames propiedad se establece con una lista separada por comas de nombres de campo que representan la clave principal del origen de datos GridView , el control crea DataKey automáticamente un objeto para cada fila del control utilizando el valor o los valores de la propiedad campo o campos especificados.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. A DataKey continuación, los objetos se agregan a DataKeys la colección del control.The DataKey objects are then added to the control's DataKeys collection. Normalmente, la DataKeys propiedad se usa para recuperar el DataKey objeto de una fila de datos concreta en GridView el control.Normally, the DataKeys property is used to retrieve the DataKey object for a specific data row in the GridView control. Sin embargo, si solo necesita recuperar el DataKey objeto de la fila seleccionada actualmente, puede usar simplemente la SelectedDataKey propiedad como un acceso directo.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. Como método abreviado adicional, puede determinar directamente el valor de la clave de datos del primer campo de clave de la fila seleccionada SelectedValue mediante la propiedad.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.

Si va a crear un ControlParameter objeto y desea tener acceso a un campo de clave que no sea el primer campo SelectedDataKey , utilice la propiedad.If you are creating a ControlParameter object and want to access a key field other than the first field, use the SelectedDataKey property. Para obtener un ejemplo, consulte SelectedDataKey.For an example, see SelectedDataKey.

Se aplica a

Consulte también: