System.Data Namespace DataColumn Class
Returns or sets a value indicating whether the column automatically increments the value of the column for new rows added to the table.
Script |
DataColumn.AutoIncrement [ = true | false ] |
This property accepts or returns only a boolean value: true if the value of the column increments automatically; otherwise, false. The default is false.
Exception Type |
Condition |
ArgumentException |
Occurs when 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 in the column's position in the array. For more details, see the DataRow class's ItemArray property.
The following example sets the AutoIncrement, AutoIncrementSeed, and AutoIncrementStep properties.
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" );
myTable.Columns.Add ( myColumn );
}
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 New DataTable ( "MyTable" )
myTable.Columns.Add ( myColumn )
End Sub |
|
C# |
VB |
DataColumn Members AutoIncrementSeed AutoIncrementStep Expression ItemArray NewRow