DataKeyCollection Class
Collapse the table of content
Expand the table of content

DataKeyCollection Class


Represents a collection that contains the key field of each record in a data source. This class cannot be inherited.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)


public sealed class DataKeyCollection : ICollection, IEnumerable


Initializes a new instance of the DataKeyCollection class.


Gets the number of items in the collection.


Gets a value indicating whether items in the DataKeyCollection can be modified.


Gets a value indicating whether the DataKeyCollection is synchronized (thread-safe).


Gets the key field at the specified index in the collection.


Gets the object used to synchronize access to the DataKeyCollection.

System_CAPS_pubmethodCopyTo(Array, Int32)

Copies all the items from the DataKeyCollection to the specified System.Array object, starting at the specified index in the System.Array object.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Creates a System.Collections.IEnumerator implemented object that contains all key fields in the DataKeyCollection.


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)


Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)


Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)


Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)


Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

The DataKeyCollection class represents a collection of the key fields in the data source. The key field of each record in the data source is stored in this collection. This allows you to store the key field with a data listing control without displaying it in the control. This collection is automatically filled with the values from the field specified by the BaseDataList.DataKeyField property. This collection does not allow you to manually add or remove items from the collection.

The key field is commonly used in a handler for an event, such as ItemCommand or DeleteCommand, as part of an update query string to revise a specific record in the data source. The key field helps the update query string identify the appropriate record to modify.

Use the Count property to determine the number of items in the collection. To programmatically retrieve a key field from a DataKeyCollection, use one of following methods:

  • Use the indexer to get a single key field from the collection, using array notation.

  • Use the CopyTo method to copy the contents of the collection to a System.Array object, which can then be used to get items from the collection.

  • Use the GetEnumerator method to create a System.Collections.IEnumerator implemented object, which can then be used to get items from the collection.

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >

<head runat="server">
    <title>BaseDataList DataKeys Example</title>
<script runat="server">

      ICollection CreateDataSource() 

         // Create sample data for the DataGrid control.
         DataTable dt = new DataTable();
         DataRow dr;

         // Define the columns of the table.
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
         dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

         // Define the primary key for the table as the IntegerValue 
         // column (column 0). To do this, first create an array of 
         // DataColumns to represent the primary key. The primary key can
         // consist of multiple columns, but in this example, only
         // one column is used.
         DataColumn[] keys = new DataColumn[1];
         keys[0] = dt.Columns[0];

         // Then assign the array to the PrimaryKey property of the DataTable. 
         dt.PrimaryKey = keys;

         // Populate the table with sample values.
         for (int i = 0; i < 9; i++) 
            dr = dt.NewRow();

            dr[0] = i;
            dr[1] = "Item " + i.ToString();
            dr[2] = 1.23 * (i + 1);


         // To persist the data source between posts to the server, 
         // store it in session state.  
         Session["Source"] = dt;

         DataView dv = new DataView(dt);
         return dv;


      void Page_Load(Object sender, EventArgs e) 

         // Load sample data only once, when the page is first loaded.
         if (!IsPostBack) 
            ItemsGrid.DataSource = CreateDataSource();


      void Delete_Command(Object sender, DataGridCommandEventArgs e)

         // Retrieve the data table from session state.
         DataTable dt = (DataTable)Session["Source"];

         // Retrieve the data row to delete from the data table. 
         // Use the DataKeys property of the DataGrid control to get 
         // the primary key value of the selected row. 
         // Search the Rows collection of the data table for this value. 
         DataRow row;
         row = dt.Rows.Find(ItemsGrid.DataKeys[e.Item.ItemIndex]);

         // Delete the item selected in the DataGrid from the data source.
         if(row != null)

         // Save the data source.
         Session["Source"] = dt;

         // Create a DataView and bind it to the DataGrid control.
         DataView dv = new DataView(dt);
         ItemsGrid.DataSource = dv;





   <form id="form1" runat="server">

      <h3>BaseDataList DataKeys Example</h3>

      <asp:DataGrid id="ItemsGrid" 


            <asp:ButtonColumn Text="Delete"





.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
© 2015 Microsoft