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 [C#] public bool AutoIncrement {get; set;} [C++] public: __property bool get_AutoIncrement(); public: __property void set_AutoIncrement(bool); [JScript] public function get AutoIncrement() : Boolean; public function set AutoIncrement(Boolean);
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 thrown 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 the method 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 (in Visual Basic Nothing) in the column's position in the array. For more details, see the DataRow class's ItemArray property.
The following example checks the AutoIncrementSupported property before setting the AutoIncrement, AutoIncrementSeed, and AutoIncrementStep properties.
[Visual Basic]
Private Sub AddAutoIncrementColumn() Dim myColumn As DataColumn = New DataColumn myColumn.DataType = System.Type.GetType("System.Int32") If myColumn.AutoIncrementSupported Then With MyColumn .AutoIncrement = True .AutoIncrementSeed = 1000 .AutoIncrementStep = 10 End With End If ' Add the column to a new DataTable Dim myTable As DataTable myTable = New DataTable myTable.Columns.Add(myColumn) End Sub
DataColumn Class | DataColumn Members | System.Data Namespace | AutoIncrementSeed | AutoIncrementStep | AutoIncrementSupported | DataRow | Expression | ItemArray | NewRow