System.Data Namespace DataColumn Class
Sets or retrieves a value indicating whether the values in each row of the column must be unique.
Script |
DataColumn.Unique = [ true | false ] |
This property accepts or returns only a boolean value: true if the value must be unique; otherwise, false. The default is false.
Exception Type |
Condition |
ArgumentException |
Occurs when the column is a calculated column. |
Once this property is set, a unique constraint will be created on this column to ensure values are unique.
Alternatively, you can also add a UniqueConstraint to the DataTable to which this column belongs to ensure the values are unique.
The following example initializes new DataColumn, sets its properties, and adds it to a table's columns collection.
private void AddColumn ( DataTable myTable ) {
// add a DataColumn to the collection and set its properties.
DataColumn myColumn;
// the constructor sets the ColumnName of the column.
myColumn = new DataColumn ( "Total" );
myColumn.DataType = System.Type.GetType ( "System.Decimal" );
myColumn.ReadOnly = true;
myColumn.Expression = "UnitPrice * Quantity";
myColumn.Unique = false;
}
Private Sub AddColumn ( myTable As DataTable )
' add a DataColumn to the collection and set its properties.
Dim myColumn As DataColumn
' the constructor sets the ColumnName of the column.
myColumn = New DataColumn ( "Total" )
myColumn.DataType = System.Type.GetType ( "System.Decimal" )
myColumn.ReadOnly = True
myColumn.Expression = "UnitPrice * Quantity"
myColumn.Unique = False
End Sub |
|
C# |
VB |
DataColumn Members AllowDBNull UniqueConstraint Constraints