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

Definizione

Ottiene il valore della chiave dati della riga selezionata in un controllo 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

Valore della proprietà

Il valore della chiave dati della riga selezionata in un controllo GridView.The data key value of the selected row in a GridView control.

Esempi

Nell'esempio seguente viene illustrato come utilizzare la SelectedValue proprietà per determinare il valore della chiave di dati della riga selezionata in GridView un controllo.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>

Commenti

Quando la DataKeyNames proprietà viene impostata con un elenco delimitato da virgole di nomi di campo che rappresentano la chiave primaria dell'origine dati GridView , il controllo Crea DataKey automaticamente un oggetto per ogni riga nel controllo utilizzando il valore o i valori di campo o campi specificati.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. Gli DataKey oggetti vengono quindi aggiunti alla DataKeys raccolta del controllo.The DataKey objects are then added to the control's DataKeys collection. In genere, DataKeys la proprietà viene utilizzata per recuperare DataKey l'oggetto per una GridView specifica riga di dati nel controllo.Normally, the DataKeys property is used to retrieve the DataKey object for a specific data row in the GridView control. Tuttavia, se è necessario recuperare solo l' DataKey oggetto della riga attualmente selezionata, è possibile usare semplicemente la SelectedDataKey proprietà come collegamento.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. Come ulteriore collegamento, è possibile determinare direttamente il valore della chiave di dati del primo campo chiave della riga selezionata utilizzando la SelectedValue proprietà.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 si sta creando un ControlParameter oggetto e si desidera accedere a un campo chiave diverso dal primo, utilizzare la SelectedDataKey proprietà.If you are creating a ControlParameter object and want to access a key field other than the first field, use the SelectedDataKey property. Per un esempio, vedere SelectedDataKey.For an example, see SelectedDataKey.

Si applica a

Vedi anche