jump to navigation

Primary Keys on Home Grown Data Tables November 19 2004

Suppose you want to create your own data table programmatically as I have previously posted

But now suppose you want to assign a Primary Key.

The primary key has many uses. The one use that sticks out in my mind is to retrieve all the data from a datatable row as in my Recovering Datagrid Data with Datakeys post.

The method “dt.PrimaryKey” requires a one dimensional array of columns in order to assign the primary key. The reason for this is to create the key from more than one column.

If you want just one key? Just create a single dimensional array and put the key column as the first value

Dim dcs(1) As DataColumn
dcs(0) = dcPrimary
dt.PrimaryKey = dcs

Just keep this in mind if you ever get a primary key missing exception :)

Comments

Sorry comments are closed for this entry