GridView.SelectedDataKey プロパティ

定義

DataKey コントロールの選択されている行のデータ キー値が格納された GridView オブジェクトを取得します。

public:
 virtual property System::Web::UI::WebControls::DataKey ^ SelectedDataKey { System::Web::UI::WebControls::DataKey ^ get(); };
[System.ComponentModel.Browsable(false)]
public virtual System.Web.UI.WebControls.DataKey SelectedDataKey { get; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedDataKey : System.Web.UI.WebControls.DataKey
Public Overridable ReadOnly Property SelectedDataKey As DataKey

プロパティ値

DataKey コントロールの選択されている行の GridView。 既定値は null です。現在、行が選択されていないことを示します。

属性

例外

データ キーが DataKeyNames プロパティに指定されていません。

次の例では、 プロパティを使用 SelectedDataKey して、コントロール内の選択した行のデータ キー値を決定する方法を GridView 示します。


<%@ 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.SelectedDataKey.Value.ToString() + ".";
    
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView SelectedDataKey Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView SelectedDataKey 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.SelectedDataKey.Value.ToString() & "."
    
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView SelectedDataKey Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView SelectedDataKey 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>

次の例では、マスター/詳細シナリオで 2 番目のキー フィールドをパラメーターとして使用する方法を示します。 GridViewコントロールは、Northwind データベースの Order Details テーブルのレコードを表示するために使用されます。 コントロールで GridView レコードを選択すると、Products テーブルの製品の詳細がコントロールに DetailsView 表示されます。 ProductID は、コントロール内の 2 番目の GridView キー名です。 2 番目のキーにアクセスするには、GridView1.SelectedDataKey[1] の値がコントロールのコントロールの オブジェクトの SqlDataSourceDetailsView として使用PropertyNameControlParameterされます。


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>Selecting Data Key Values</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:SqlDataSource 
      ID="SqlDataSource1" 
      runat="server" 
      ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>"
      ProviderName="System.Data.SqlClient" 
      SelectCommand="SELECT * FROM [Order Details]">
    </asp:SqlDataSource>

    <asp:SqlDataSource 
      ID="SqlDataSource2" 
      runat="server" 
      ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>"
      ProviderName="System.Data.SqlClient" 
      SelectCommand="SELECT * FROM [Products]WHERE [ProductID] = @productid">
      <SelectParameters>
        <asp:ControlParameter 
          Name="productid" 
          ControlID="GridView1" 
          PropertyName="SelectedDataKey[1]" />
      </SelectParameters>
    </asp:SqlDataSource>
    
  </div>

  <asp:GridView 
    ID="GridView1" 
    runat="server" 
    AllowPaging="True" 
    AutoGenerateColumns="False"
    DataKeyNames="OrderID,ProductID" DataSourceID="SqlDataSource1">
    <Columns>
      <asp:CommandField ShowSelectButton="True" />
      <asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True"/>
      <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" />
      <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
      <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
      <asp:BoundField DataField="Discount" HeaderText="Discount" />
      </Columns>
    </asp:GridView>
    <br />
    <asp:DetailsView 
      ID="DetailsView1" 
      runat="server" 
      AutoGenerateRows="False" 
      DataKeyNames="ProductID"
      DataSourceID="SqlDataSource2" 
      Height="50px" Width="125px">
      <Fields>
        <asp:BoundField 
          DataField="ProductID" 
          HeaderText="ProductID" 
          InsertVisible="False"
          ReadOnly="True" />
        <asp:BoundField DataField="ProductName" HeaderText="ProductName"/>
        <asp:BoundField DataField="SupplierID" HeaderText="SupplierID"/>
        <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
        <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" />
        <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
        <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" />
        <asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" />
        <asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" />
        <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" />
      </Fields>
    </asp:DetailsView>
  </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">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>Selecting Data Key Values</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:SqlDataSource 
      ID="SqlDataSource1" 
      runat="server" 
      ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>"
      ProviderName="System.Data.SqlClient" 
      SelectCommand="SELECT * FROM [Order Details]">
    </asp:SqlDataSource>

    <asp:SqlDataSource 
      ID="SqlDataSource2" 
      runat="server" 
      ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>"
      ProviderName="System.Data.SqlClient" 
      SelectCommand="SELECT * FROM [Products]WHERE [ProductID] = @productid">
      <SelectParameters>
        <asp:ControlParameter 
          Name="productid" 
          ControlID="GridView1" 
          PropertyName="SelectedDataKey[1]" />
      </SelectParameters>
    </asp:SqlDataSource>
    
  </div>

  <asp:GridView 
    ID="GridView1" 
    runat="server" 
    AllowPaging="True" 
    AutoGenerateColumns="False"
    DataKeyNames="OrderID,ProductID" DataSourceID="SqlDataSource1">
    <Columns>
      <asp:CommandField ShowSelectButton="True" />
      <asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True"/>
      <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" />
      <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
      <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
      <asp:BoundField DataField="Discount" HeaderText="Discount" />
      </Columns>
    </asp:GridView>
    <br />
    <asp:DetailsView 
      ID="DetailsView1" 
      runat="server" 
      AutoGenerateRows="False" 
      DataKeyNames="ProductID"
      DataSourceID="SqlDataSource2" 
      Height="50px" Width="125px">
      <Fields>
        <asp:BoundField 
          DataField="ProductID" 
          HeaderText="ProductID" 
          InsertVisible="False"
          ReadOnly="True" />
        <asp:BoundField DataField="ProductName" HeaderText="ProductName"/>
        <asp:BoundField DataField="SupplierID" HeaderText="SupplierID"/>
        <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
        <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" />
        <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
        <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" />
        <asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" />
        <asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" />
        <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" />
      </Fields>
    </asp:DetailsView>
  </form>
</body>
</html>

注釈

プロパティが DataKeyNames 設定されると、コントロールは、 GridView 指定したフィールドまたはフィールドの値または値を使用して、コントロール内の各行の オブジェクトを自動的に作成 DataKey します。 DataKey コントロールが使用するコントロールのコレクションに追加される DataKeys。 通常、 DataKeys プロパティは、コントロール内GridViewの特定のデータ行のDataKeyオブジェクトを取得するために使用されます。 ただし、現在選択されている行のオブジェクトを DataKey 取得するだけで済む場合は、 プロパティを SelectedDataKey ショートカットとして使用できます。

注意

これは、 プロパティで指定されたインデックスにある オブジェクトをDataKeyコレクションから取得するのSelectedIndexDataKeysと同じです。 プロパティを SelectedValue 使用して、現在選択されている行のデータ キー値を直接取得することもできます。

オブジェクトを作成していてControlParameter、最初のフィールド以外のキー フィールドにアクセスする場合は、 オブジェクトの プロパティでインデックス付きSelectedDataKeyプロパティをPropertyNameControlParameter使用します。 次に例を示します。

適用対象

こちらもご覧ください