GridView.DataKeyNames 属性

定义

获取或设置一个数组,该数组包含了显示在 GridView 控件中的项的主键字段的名称。Gets or sets an array that contains the names of the primary key fields for the items displayed in a GridView control.

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; }
member this.DataKeyNames : string[] with get, set
Public Overridable Property DataKeyNames As String()

属性值

String[]

一个数组,其中包含了显示在 GridView 控件中的项的主键字段的名称。An array that contains the names of the primary key fields for the items displayed in a GridView control.

实现

属性

示例

下面的示例演示如何使用DataKeyNames属性指定数据源的键字段。The following example demonstrates how to use the DataKeyNames property to specify the key field of the data source. 在此示例中, DataKeyNames标记中GridView元素的属性通过使用逗号分隔名称来指定两个键字段。In the example, the DataKeyNames attribute of the GridView element in markup specifies two key fields by using a comma to separate the names. 若要运行此示例, 请创建一个具有以下内容的网站:To run this example, create a Web site that has the following:

  • 与 AdventureWorksLT 示例数据库的连接, 以及一个名AdventureWorksLTConnectionString为的连接字符串。A connection to the AdventureWorksLT sample database and a connection string that is named AdventureWorksLTConnectionString. 有关如何设置 AdventureWorksLT 示例数据库的信息, 请参阅如何:设置用于 ASP.NET 开发的 AdventureWorksLT 示例数据库。For information about how to set up the AdventureWorksLT sample database, see How to: Set Up an AdventureWorksLT Sample Database for ASP.NET Development.

  • AdventureWorksLTDataClassesDataContext为的 LINQ 到 SQL 数据上下文类。A LINQ-to-SQL data context class that is named AdventureWorksLTDataClassesDataContext. 数据上下文必须具有 SalesOrderDetails 表的类。The data context must have a class for the SalesOrderDetails table. 有关如何创建 LINQ to SQL 类的信息, 请参阅LINQ to SQLFor information about how to create LINQ-to-SQL classes, see 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使用属性指定表示数据源的主键的一个或多个字段。Use the DataKeyNames property to specify the field or fields that represent the primary key of the data source. 只应将此属性设置为唯一标识每行所需的一个或多个字段;例如, 如果整数值唯一地标识每一行, 则为 ID 列。You should only set this property to the field or fields that are required to uniquely identify each row; for example, the ID column if an integer value uniquely identifies each row. 必须设置DataKeyNames属性才能使GridView控件的 "自动更新" 和 "删除" 功能生效。You must set the DataKeyNames property in order for the automatic update and delete features of the GridView control to work. 这些键字段的值将传递到数据源控件, 以便指定要更新或删除的行。The values of these key fields are passed to the data source control in order to specify the row to update or delete.

如果在更新或删除行时需要检索数据密钥值, 请使用Keys GridViewUpdateEventArgsGridViewDeleteEventArgs类的属性。If you need to retrieve the data key value when updating or deleting a row, use the Keys property of either the GridViewUpdateEventArgs or GridViewDeleteEventArgs class. 例如, e.Keys[0]保存RowUpdatingRowDeleting事件处理程序中的第一个数据键的值。For example, e.Keys[0] holds the value of the first data key in a RowUpdating or RowDeleting event handler.

如果在选择行时需要检索数据键值, 请使用SelectedDataKey属性。If you need to retrieve the data key value when a row is selected, use the SelectedDataKey property.

设置属性后GridView , 控件将使用指定字段中的DataKeys值自动填充其集合, 这提供了一种便捷的方式来访问每行的主键。 DataKeyNamesWhen the DataKeyNames property is set, the GridView control automatically populates its DataKeys collection with the values from the specified field or fields, which provides a convenient way to access the primary keys of each row.

备注

GridView控件将这些键字段值存储在控件状态中。The GridView control stores these key field values in the control state. 如果这些值包含敏感信息, 则强烈建议您通过将ViewStateEncryptionMode属性设置为来ViewStateEncryptionMode.Always启用视图状态加密。If these values contain sensitive information, it is strongly recommended that you enable view-state encryption by setting the ViewStateEncryptionMode property to ViewStateEncryptionMode.Always.

当使用自动生成的字段AutoGenerateColumns列 (通过将属性设置为true) 时, GridView控件将确保与DataKeyNames属性中指定的字段对应的列是只读的。When you use automatically generated field columns (by setting the AutoGenerateColumns property to true), the GridView control makes sure that the columns that correspond to the field or fields specified in the DataKeyNames property are read-only.

如果列Visible字段的属性设置为false, 则该列不GridView会显示在控件中, 并且列的数据不会对客户端进行往返。If the Visible property of a column field is set to false, the column is not displayed in the GridView control and the data for the column does not make a round trip to the client. 如果希望不可见的列的数据可供客户端使用, 请将字段名称添加到DataKeyNames属性。If you want the data for a column that is not visible to be available to the client, add the field name to the DataKeyNames property.

适用于

另请参阅