GridView.DataKeyNames プロパティ

定義

GridView コントロールに表示される項目の主キー フィールドの名前が格納された配列を取得または設定します。

public:
 virtual property cli::array <System::String ^> ^ DataKeyNames { cli::array <System::String ^> ^ get(); void set(cli::array <System::String ^> ^ value); };
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.StringArrayConverter))]
public virtual string[] DataKeyNames { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.StringArrayConverter))>]
member this.DataKeyNames : string[] with get, set
Public Overridable Property DataKeyNames As String()

プロパティ値

String[]

GridView コントロールに表示される項目の主キー フィールドの名前が格納された配列。

属性

次の例では、 プロパティを使用 DataKeyNames してデータ ソースのキー フィールドを指定する方法を示します。 この例では、 DataKeyNames マークアップの 要素の 属性で、名前を GridView 区切るためにコンマを使用して 2 つのキー フィールドを指定しています。 この例を実行するには、次の Web サイトを作成します。

  • AdventureWorksLT サンプル データベースへの接続と、 という名前 AdventureWorksLTConnectionStringの接続文字列。 AdventureWorksLT サンプル データベースを設定する方法については、「 How to: Set up an AdventureWorksLT Sample Database for ASP.NET Development」を参照してください。

  • という名前 AdventureWorksLTDataClassesDataContextの LINQ-to-SQL データ コンテキスト クラス。 データ コンテキストには、SalesOrderDetails テーブルの クラスが必要です。 LINQ-to-SQL クラスを作成する方法については、「LINQ to SQL」を参照してください。

<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="AdventureWorksLTDataClassesDataContext"
    EnableDelete="True" EnableInsert="True" EnableUpdate="True" 
    TableName="SalesOrderDetails">
</asp:LinqDataSource>

<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False" 
    DataKeyNames="SalesOrderID,SalesOrderDetailID"
    DataSourceID="LinqDataSource1">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" 
            ShowEditButton="True" />
        <asp:BoundField DataField="SalesOrderID" 
            HeaderText="SalesOrderID" ReadOnly="True"
            SortExpression="SalesOrderID" />
        <asp:BoundField DataField="SalesOrderDetailID" 
            HeaderText="SalesOrderDetailID" InsertVisible="False"
            ReadOnly="True" SortExpression="SalesOrderDetailID" />
        <asp:BoundField DataField="OrderQty" 
            HeaderText="OrderQty" SortExpression="OrderQty" />
        <asp:BoundField DataField="ProductID" 
            HeaderText="ProductID" SortExpression="ProductID" />
        <asp:BoundField DataField="UnitPrice" 
            HeaderText="UnitPrice" SortExpression="UnitPrice" />
        <asp:BoundField DataField="ModifiedDate" 
            HeaderText="ModifiedDate" SortExpression="ModifiedDate" />
    </Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="AdventureWorksLTDataClassesDataContext"
    EnableDelete="True" EnableInsert="True" EnableUpdate="True" 
    TableName="SalesOrderDetails">
</asp:LinqDataSource>

<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False" 
    DataKeyNames="SalesOrderID,SalesOrderDetailID"
    DataSourceID="LinqDataSource1">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" 
            ShowEditButton="True" />
        <asp:BoundField DataField="SalesOrderID" 
            HeaderText="SalesOrderID" ReadOnly="True"
            SortExpression="SalesOrderID" />
        <asp:BoundField DataField="SalesOrderDetailID" 
            HeaderText="SalesOrderDetailID" InsertVisible="False"
            ReadOnly="True" SortExpression="SalesOrderDetailID" />
        <asp:BoundField DataField="OrderQty" 
            HeaderText="OrderQty" SortExpression="OrderQty" />
        <asp:BoundField DataField="ProductID" 
            HeaderText="ProductID" SortExpression="ProductID" />
        <asp:BoundField DataField="UnitPrice" 
            HeaderText="UnitPrice" SortExpression="UnitPrice" />
        <asp:BoundField DataField="ModifiedDate" 
            HeaderText="ModifiedDate" SortExpression="ModifiedDate" />
    </Columns>
</asp:GridView>

注釈

プロパティを DataKeyNames 使用して、データ ソースの主キーを表すフィールドを指定します。 このプロパティは、各行を一意に識別するために必要なフィールドにのみ設定する必要があります。たとえば、整数値が各行を一意に識別する場合の ID 列です。 コントロールの DataKeyNames 自動更新機能と削除機能を機能させるには、 プロパティを設定する GridView 必要があります。 これらのキー フィールドの値は、更新または削除する行を指定するためにデータ ソース コントロールに渡されます。

行を更新または削除するときにデータ キー値を取得する必要がある場合は、 クラスまたは GridViewDeleteEventArgs クラスの プロパティをGridViewUpdateEventArgs使用Keysします。 たとえば、 e.Keys[0] は、 または RowDeleting イベント ハンドラーの最初のデータ キーの値をRowUpdating保持します。

行を選択するときにデータ キー値を取得する必要がある場合は、 プロパティを SelectedDataKey 使用します。

プロパティを DataKeyNames 設定すると、 GridView コントロールは、指定されたフィールドまたはフィールドの値をコレクションに自動的に設定 DataKeys します。これにより、各行の主キーに簡単にアクセスできます。

Note

コントロールは GridView 、これらのキー フィールド値をコントロール状態に格納します。 これらの値に機密情報が含まれている場合は、 プロパティViewStateEncryptionMode.Alwaysを に設定してビューステート暗号化をViewStateEncryptionMode有効にすることを強くお勧めします。

自動的に生成されるフィールド列を使用する場合 (プロパティを AutoGenerateColumnstrue設定することによって) GridView 、コントロールは、プロパティで DataKeyNames 指定されたフィールドまたはフィールドに対応する列が読み取り専用であることを確認します。

Visible列フィールドの プロパティが にfalse設定されている場合、列はコントロールにGridView表示されず、列のデータはクライアントへのラウンド トリップを行いません。 表示されない列のデータをクライアントで使用できるようにする場合は、フィールド名を プロパティに DataKeyNames 追加します。

適用対象

こちらもご覧ください