GridView.DataKeys プロパティ

定義

DataKey コントロールの各行のデータ キー値を表す GridView オブジェクトのコレクションを取得します。

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

プロパティ値

DataKeyArray コントロールの各行のデータ キーが格納された GridView

属性

次の例では、 プロパティを DataKeys 使用して、コントロール内の選択した行のデータ キー値を決定する方法を 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)
  {
    
    // Determine the index of the selected row.
    int index = CustomersGridView.SelectedIndex;
        
    // Display the primary key value of the selected row.
    Message.Text = "The primary key value of the selected row is " +
        CustomersGridView.DataKeys[index].Value.ToString() + ".";
    
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView DataKeys Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView DataKeys Example</h3>

      <asp:label id="Message"
        forecolor="Red"
        runat="server"/>
               
      <br/><br/>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        autogenerateselectbutton="true"    
        datakeynames="CustomerID"
        onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
        runat="server">
                
      </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)
      
    ' Determine the index of the selected row.
    Dim index As Integer = CustomersGridView.SelectedIndex
        
    ' Display the primary key value of the selected row.
    Message.Text = "The primary key value of the selected row is " & _
        CustomersGridView.DataKeys(index).Value.ToString() & "."
    
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView DataKeys Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView DataKeys Example</h3>

      <asp:label id="Message"
        forecolor="Red"
        runat="server"/>
               
      <br/><br/>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        autogenerateselectbutton="true"    
        datakeynames="CustomerID"
        onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
        runat="server">
                
      </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>

注釈

プロパティが DataKeyNames 設定されると、コントロールはコントロール内 GridViewDataKey 各行にオブジェクトを自動的に作成します。 オブジェクトには DataKey 、 プロパティで指定されたフィールドの値が DataKeyNames 含まれます。 DataKey コントロールが使用するコントロールのコレクションに追加される DataKeys。 コントロール内の DataKeys 特定のデータ行の DataKey オブジェクトを取得するには、 プロパティを GridView 使用します。

注意

プロパティを SelectedDataKey 使用すると、現在選択されている行の DataKey オブジェクトを取得できます。 プロパティを SelectedValue 使用して、現在選択されている行のデータ キー値を直接取得することもできます。

適用対象

こちらもご覧ください