GridView.SelectedValue Propriedade

Definição

Obtém o valor da chave de dados da linha selecionada em um controle 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; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedValue : obj
Public ReadOnly Property SelectedValue As Object

Valor da propriedade

Object

O valor da chave de dados da linha selecionada em um controle GridView.The data key value of the selected row in a GridView control.

Atributos

Exemplos

O exemplo a seguir demonstra como usar a SelectedValue propriedade para determinar o valor da chave de dados da linha selecionada em um GridView controle.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>

Comentários

Quando a DataKeyNames propriedade é definida com uma lista separada por vírgulas de nomes de campo que representam a chave primária da fonte de dados, o GridView controle cria automaticamente um DataKey objeto para cada linha no controle usando o valor ou os valores do campo ou dos 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. Os DataKey objetos são então adicionados à coleção do controle DataKeys .The DataKey objects are then added to the control's DataKeys collection. Normalmente, a DataKeys propriedade é usada para recuperar o DataKey objeto para uma linha de dados específica no GridView controle.Normally, the DataKeys property is used to retrieve the DataKey object for a specific data row in the GridView control. No entanto, se você só precisar recuperar o DataKey objeto da linha atualmente selecionada, poderá simplesmente usar a SelectedDataKey propriedade como um atalho.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 um atalho adicional, você pode determinar diretamente o valor da chave de dados do primeiro campo de chave da linha selecionada usando a SelectedValue propriedade.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.

Se você estiver criando um ControlParameter objeto e quiser acessar um campo de chave diferente do primeiro campo, use a SelectedDataKey propriedade.If you are creating a ControlParameter object and want to access a key field other than the first field, use the SelectedDataKey property. Para ver um exemplo, consulte SelectedDataKey.For an example, see SelectedDataKey.

Aplica-se a

Confira também