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

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

DataKeyNames プロパティが設定されている場合、GridView コントロールは、指定した 1 つ以上のフィールドの値を使用して、コントロールの各行の DataKey オブジェクトを自動作成します。その後、DataKey オブジェクトは、コントロールの DataKeys コレクションに追加されます。通常、DataKeys プロパティは、GridView コントロールの特定のデータ行の DataKey オブジェクトを取得するために使用されます。ただし、現在選択されている行の DataKey オブジェクトだけを取得する必要がある場合は、より簡単な方法として、SelectedDataKey プロパティを使用することもできます。
![]() |
---|
これは、DataKeys コレクションから、SelectedIndex プロパティで指定されたインデックス位置にある DataKey オブジェクトを取得することと同じです。また、SelectedValue プロパティを使用して、現在選択されている行のデータ キー値を直接取得することもできます。 |
ControlParameter オブジェクトを作成するときに最初のフィールド以外のキー フィールドにアクセスする場合は、ControlParameter オブジェクトの PropertyName プロパティでインデックス付きの SelectedDataKey プロパティを使用します。次に例を示します。

SelectedDataKey プロパティを使用して、GridView コントロールの選択されている行のデータ キー値を確認する方法を次のコード例に示します。
<%@ Page language="VB" %> <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> <body> <form 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="C#" %> <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> <body> <form 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 は、GridView コントロール内の 2 番目のキー名です。2 番目のキーにアクセスするために、DetailsView コントロールの SqlDataSource コントロールの ControlParameter オブジェクトに対する PropertyName の値として GridView1.SelectedDataKey[1] が使用されます。
<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <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="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <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>

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.SelectedDataKey プロパティを検索する場合は、下記のリンクをクリックしてください。

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