GridView.DataKeys プロパティ
アセンブリ: System.Web (system.web.dll 内)

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

DataKeyNames プロパティが設定されている場合、GridView コントロールは、コントロール内の各行の DataKey オブジェクトを自動作成します。DataKey オブジェクトには、DataKeyNames プロパティで指定された 1 つ以上のフィールドの値が格納されます。その後、DataKey オブジェクトは、コントロールの DataKeys コレクションに追加されます。DataKeys プロパティを使用して、GridView コントロールの特定のデータ行の DataKey オブジェクトを取得します。
![]() |
---|
SelectedDataKey プロパティを使用して、現在選択されている行の DataKey オブジェクトを取得できます。また、SelectedValue プロパティを使用して、現在選択されている行のデータ キー値を直接取得することもできます。 |

DataKeys プロパティを使用して、GridView コントロールの選択されている行のデータ キー値を確認する方法を次のコード例に示します。
<%@ Page language="VB" %> <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> <body> <form 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="C#" %> <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> <body> <form 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>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からGridView.DataKeys プロパティを検索する場合は、下記のリンクをクリックしてください。

- GridView.DataKeys プロパティのページへのリンク