Export (0) Print
Expand All

DataColumn.AutoIncrement Property

Gets or sets a value that indicates whether the column automatically increments the value of the column for new rows added to the table.

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

public bool AutoIncrement { get; set; }
/** @property */
public boolean get_AutoIncrement ()

/** @property */
public void set_AutoIncrement (boolean value)

public function get AutoIncrement () : boolean

public function set AutoIncrement (value : boolean)

Not applicable.

Property Value

true if the value of the column increments automatically; otherwise, false. The default is false.

Exception typeCondition

ArgumentException

The column is a computed column.

If the type of this column is not Int16, Int32, or Int64 when this property is set, the DataType property is coerced to Int32. An exception is generated if this is a computed column. The Expression property is set. The incremented value is used only if the row's value for this column, when added to the columns collection, is equal to the default value.

You can create a new row using the ItemArray property of the DataRow class and passing in an array of values. This is a potential problem for a column with its AutoIncrement set to true, because its value is generated automatically. To use the ItemArray property, place a null reference (Nothing in Visual Basic) in the column's position in the array. For more information, see the ItemArray property of the DataRow class.

The following example sets the AutoIncrement, AutoIncrementSeed, and AutoIncrementStep properties.

private void AddAutoIncrementColumn()
{
    DataColumn column = new DataColumn();
    column.DataType = System.Type.GetType("System.Int32");
    column.AutoIncrement = true;
    column.AutoIncrementSeed = 1000;
    column.AutoIncrementStep = 10;

    // Add the column to a new DataTable.
    DataTable table = new DataTable("table");
    table.Columns.Add(column);
}

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

Community Additions

ADD
Show:
© 2014 Microsoft