Bewerken

Delen via


GridView.DataKeyNames Property

Definition

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; }
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.StringArrayConverter))>]
member this.DataKeyNames : string[] with get, set
Public Overridable Property DataKeyNames As String()

Property Value

String[]

An array that contains the names of the primary key fields for the items displayed in a GridView control.

Attributes

Examples

The following example demonstrates how to use the DataKeyNames property to specify the key field of the data source. 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:

  • A connection to the AdventureWorksLT sample database and a connection string that is named AdventureWorksLTConnectionString. For information about how to set up the AdventureWorksLT sample database, see How to: Set Up an AdventureWorksLT Sample Database for ASP.NET Development.

  • A LINQ-to-SQL data context class that is named AdventureWorksLTDataClassesDataContext. The data context must have a class for the SalesOrderDetails table. For 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>

Remarks

Use the DataKeyNames property to specify the field or fields that represent the primary key of the data source. 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. 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.

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. For example, e.Keys[0] holds the value of the first data key in a RowUpdating or RowDeleting event handler.

If you need to retrieve the data key value when a row is selected, use the SelectedDataKey property.

When 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.

Note

The GridView control stores these key field values in the control state. If these values contain sensitive information, it is strongly recommended that you enable view-state encryption by setting the ViewStateEncryptionMode property to ViewStateEncryptionMode.Always.

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.

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. 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.

Applies to

See also