GridView.SelectedValue プロパティ
メモ : このプロパティは、.NET Framework version 2.0 で新しく追加されたものです。
GridView コントロールの選択されている行のデータ キー値を取得します。
名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文

DataKeyNames プロパティにデータ ソースの主キーを表すフィールド名のコンマ区切りのリストが設定されている場合、GridView コントロールは、指定した 1 つ以上のフィールドの値を使用して、各行の DataKey オブジェクトを自動作成します。その後、DataKey オブジェクトは、コントロールの DataKeys コレクションに追加されます。通常、DataKeys プロパティは、GridView コントロールの特定のデータ行の DataKey オブジェクトを取得するために使用されます。ただし、現在選択されている行の DataKey オブジェクトだけを取得する必要がある場合は、より簡単な方法として、SelectedDataKey プロパティを使用することもできます。より簡単な方法として、SelectedValue プロパティを使用して、選択されている行の最初のキー フィールドのデータ キー値を直接確認することもできます。
ControlParameter オブジェクトを作成するときに 1 番目のフィールド以外のキー フィールドにアクセスする場合は、SelectedDataKey プロパティを使用します。例については、SelectedDataKey のトピックを参照してください。

SelectedValue プロパティを使用して、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.SelectedValue.ToString() & "." End Sub </script> <html> <body> <form 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="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.SelectedValue.ToString() + "."; } </script> <html> <body> <form 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>

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

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