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

Dim instance As GridView Dim value As String() value = instance.DataKeyNames instance.DataKeyNames = value
public: virtual property array<String^>^ DataKeyNames { array<String^>^ get (); void set (array<String^>^ value); }
/** @property */ public String[] get_DataKeyNames () /** @property */ public void set_DataKeyNames (String[] value)
GridView コントロールに表示される項目の主キー フィールドの名前が格納された配列。

DataKeyNames プロパティを使用して、データ ソースの主キーを表す 1 つ以上のフィールドを指定します。
![]() |
---|
DataKeyNames プロパティが設定されている場合、GridView コントロールは、指定した 1 つ以上のフィールドの値を自動的に DataKeys コレクションに読み込みます。これにより、各行の主キーに簡単にアクセスできます。
![]() |
---|
GridView コントロールでは、これらのキー フィールド値がコントロールの状態に格納されます。これらの値に機密情報が含まれている場合は、ViewStateEncryptionMode プロパティを ViewStateEncryptionMode.Always に設定して、ビューステートの暗号化を有効にするようにしてください。 |
(AutoGenerateColumns プロパティを true に設定することによって) 自動生成されたフィールド列を使用する場合、GridView コントロールは、DataKeyNames プロパティで指定された 1 つ以上のフィールドに対応する自動生成されたフィールド列が読み取り専用であるかどうかを自動的に確認します。
![]() |
---|
GridView コントロールの自動更新機能と自動削除機能が動作するようにするには、DataKeyNames プロパティを設定する必要があります。これらのキー フィールドの値は、更新または削除する行を一致させるために、データ ソース コントロールに渡されます。 |
列フィールドの Visible プロパティが false に設定されている場合、その列は GridView コントロールに表示されず、列のデータはクライアントへのラウンド トリップを行いません。表示されない列のデータをラウンド トリップさせる場合は、そのフィールド名を DataKeyNames プロパティに追加します。

DataKeyNames プロパティを使用して、データ ソースのキー フィールドを指定する方法を次のコード例に示します。
<%@ Page language="VB" %> <html> <body> <form runat="server"> <h3>GridView Edit Example</h3> <!-- The GridView control automatically sets the columns --> <!-- specified in the datakeynames property as read-only. --> <!-- No input controls are rendered for these columns in --> <!-- edit mode. --> <asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" autogeneratecolumns="true" autogeneratedeletebutton="true" autogenerateeditbutton="true" datakeynames="CustomerID" 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="CustomersSqlDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)" deletecommand="Delete from Customers where CustomerID = @CustomerID" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"> </asp:sqldatasource> </form> </body> </html>
<%@ Page language="C#" %> <html> <body> <form runat="server"> <h3>GridView Edit Example</h3> <!-- The GridView control automatically sets the columns --> <!-- specified in the datakeynames property as read-only. --> <!-- No input controls are rendered for these columns in --> <!-- edit mode. --> <asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" autogeneratecolumns="true" autogeneratedeletebutton="true" autogenerateeditbutton="true" datakeynames="CustomerID" 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="CustomersSqlDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)" deletecommand="Delete from Customers where CustomerID = @CustomerID" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"> </asp:sqldatasource> </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.DataKeyNames プロパティを検索する場合は、下記のリンクをクリックしてください。

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