Este artigo foi traduzido por máquina. Para visualizar o arquivo em inglês, marque a caixa de seleção Inglês. Você também pode exibir o texto Em inglês em uma janela pop-up, movendo o ponteiro do mouse sobre o texto.
Tradução
Inglês

Propriedade GridView.SelectedValue

 

Obtém o valor da chave de dados da linha selecionada em um controle GridView.

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

[BrowsableAttribute(false)]
public object SelectedValue { get; }

Valor da Propriedade

Type: System.Object

O valor da chave de dados da linha selecionada em um controle GridView.

When the P:System.Web.UI.WebControls.GridView.DataKeyNames property is set with a comma-separated list of field names that represent the primary key of the data source, the T:System.Web.UI.WebControls.GridView control automatically creates a T:System.Web.UI.WebControls.DataKey object for each row in the control using the value or values of the specified field or fields. The T:System.Web.UI.WebControls.DataKey objects are then added to the control's P:System.Web.UI.WebControls.GridView.DataKeys collection. Normally, the P:System.Web.UI.WebControls.GridView.DataKeys property is used to retrieve the T:System.Web.UI.WebControls.DataKey object for a specific data row in the T:System.Web.UI.WebControls.GridView control. However, if you just need to retrieve the T:System.Web.UI.WebControls.DataKey object of the currently selected row, you can simply use the P:System.Web.UI.WebControls.GridView.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 P:System.Web.UI.WebControls.GridView.SelectedValue property.

If you are creating a T:System.Web.UI.WebControls.ControlParameter object and want to access a key field other than the first field, use the P:System.Web.UI.WebControls.GridView.SelectedDataKey property. For an example, see P:System.Web.UI.WebControls.GridView.SelectedDataKey.

The following example demonstrates how to use the P:System.Web.UI.WebControls.GridView.SelectedValue property to determine the data key value of the selected row in a T:System.Web.UI.WebControls.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>

.NET Framework
Disponível desde 2.0
Retornar ao início
Mostrar: