This documentation is archived and is not being maintained.

DataColumn.AutoIncrement Property

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

[Visual Basic]
Public Property AutoIncrement As Boolean
public bool AutoIncrement {get; set;}
public: __property bool get_AutoIncrement();
public: __property void set_AutoIncrement(bool);
public function get AutoIncrement() : Boolean;
public function set AutoIncrement(Boolean);

Property Value

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


Exception Type Condition
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 (that is, 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 DataRow class's ItemArray property 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 details, see the DataRow class's ItemArray property.


[Visual Basic, C#, C++] The following example sets the AutoIncrement, AutoIncrementSeed, and AutoIncrementStep properties.

[Visual Basic] 
Private Sub AddAutoIncrementColumn()
    Dim myColumn As DataColumn = New DataColumn
    myColumn.DataType = System.Type.GetType("System.Int32")
    With MyColumn
       .AutoIncrement = True
       .AutoIncrementSeed = 1000
       .AutoIncrementStep = 10
    End With
    ' Add the column to a new DataTable.
    Dim myTable As DataTable
    myTable = New DataTable
 End Sub

private void AddAutoIncrementColumn(){
    DataColumn myColumn = new DataColumn();
    myColumn.DataType = System.Type.GetType("System.Int32");
    myColumn.AutoIncrement = true;
    myColumn.AutoIncrementSeed = 1000;
    myColumn.AutoIncrementStep = 10;
    // Add the column to a new DataTable.
    DataTable myTable = new DataTable("MyTable");

void AddAutoIncrementColumn(){
    DataColumn* myColumn = new DataColumn();
    myColumn->DataType = System::Type::GetType(S"System.Int32");
    myColumn->AutoIncrement = true;
    myColumn->AutoIncrementSeed = 1000;
    myColumn->AutoIncrementStep = 10;
    // Add the column to a new DataTable.
    DataTable* myTable = new DataTable(S"MyTable");

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataColumn Class | DataColumn Members | System.Data Namespace | AutoIncrementSeed | AutoIncrementStep | Expression | ItemArray | NewRow