AutoIncrement
Boolean autoIncrement = DataColumn.AutoIncrement; DataColumn.AutoIncrement = autoIncrement;
Gets or sets whether the column value automatically increments with each new row added to the table.
The following example shows how to set the
AutoIncrement
,
AutoIncrementSeed
, and
AutoIncrementStep
properties of the
DataColumn
:
DataColumn col = new DataColumn("MyColumn", typeof(System.Int32)); col.AutoIncrement = true; col.AutoIncrementSeed = -1; col.AutoIncrementStep = -1;
The starting value for an AutoIncrement
column in
defined by the AutoIncrementSeed
property. The
subsequent values for the column are determined by the
AutoIncrementStep
property.
If the column type isn’t an integer type (i.e.,
Int16
, Int32
, or
Int64
) and the AutoIncrement
property is set to true
, the
DataType
property of the column is coerced to
Int32
. If the column is computed (i.e.,
Expression
property is set), and an attempt is
made to set the AutoIncrement
property to
true
, an exception is raised.
A new row can be created by using the ItemArray
property of the DataRow
and passing in an array of
column values. Pass a null
reference for
AutoIncrement
columns.
This property isn’t set by calling either the
Fill( )
or FillSchema( )
of the
DataAdapter
, regardless of the settings in the
data source.
The default value of this property is false
.