Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataTable.PrimaryKey Property

Gets or sets an array of columns that function as primary keys for the data table.

Namespace: System.Data
Assembly: System.Data (in system.data.dll)

[TypeConverterAttribute(typeof(PrimaryKeyTypeConverter))] 
public DataColumn[] PrimaryKey { get; set; }
/** @property */
public DataColumn[] get_PrimaryKey ()

/** @property */
public void set_PrimaryKey (DataColumn[] value)

public function get PrimaryKey () : DataColumn[]

public function set PrimaryKey (value : DataColumn[])

Not applicable.

Property Value

An array of DataColumn objects.

Exception typeCondition

DataException

The key is a foreign key.

The primary key of a table must be unique to identify the record in the table. It's also possible to have a table with a primary key made up of two or more columns. This occurs when a single column can't contain enough unique values. For example, a two column primary key might consist of a "FirstName" and "LastName" column. Because primary keys can be made up of more than one column, the PrimaryKey property consists of an array of DataColumn objects.

The first example shows how to return the primary key columns for a DataTable displayed in a DataGrid. The second example demonstrates how to set the primary key columns for a DataTable.

private void GetPrimaryKeys(DataTable table)
{
    // Create the array for the columns.
    DataColumn[] columns;
    columns = table.PrimaryKey;

    // Get the number of elements in the array.
    Console.WriteLine("Column Count: " + columns.Length);
    for(int i = 0; i < columns.Length; i++)
    {
        Console.WriteLine(columns[i].ColumnName + columns[i].DataType);
    }
}
 
private void SetPrimaryKeys()
{
    // Create a new DataTable and set two DataColumn objects as primary keys.
    DataTable table = new DataTable();
    DataColumn[] keys = new DataColumn[1];
    DataColumn column;

    // Create column 1.
    column = new DataColumn();
    column.DataType = System.Type.GetType("System.String");
    column.ColumnName= "FirstName";

    // Add the column to the DataTable.Columns collection.
    table.Columns.Add(column);

    // Add the column to the array.
    keys[0] = column;
 
    // Create column 2 and add it to the array.
    column = new DataColumn();
    column.DataType = System.Type.GetType("System.String");
    column.ColumnName = "LastName";
    table.Columns.Add(column);

    // Add the column to the array.
    keys[1] = column;

    // Set the PrimaryKeys property to the array.
    table.PrimaryKey = keys;
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft