Export (0) Print
Expand All

DataColumn.Unique Property

Gets or sets a value indicating whether the values in each row of the column must be unique.

[Visual Basic]
Public Property Unique As Boolean
[C#]
public bool Unique {get; set;}
[C++]
public: __property bool get_Unique();
public: __property void set_Unique(bool);
[JScript]
public function get Unique() : Boolean;
public function set Unique(Boolean);

Property Value

true if the value must be unique; otherwise, false. The default is false.

Exceptions

Exception Type Condition
ArgumentException The column is a calculated column.

Remarks

Once this property is changed from false to true, a unique constraint will be created on this column to ensure values are unique.

Example

[Visual Basic, C#, C++] The following example creates new DataColumn, sets its properties, and adds it to a table's columns collection.

[Visual Basic] 
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#] 
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;
 }

[C++] 
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(S"Total");
    myColumn->DataType = System::Type::GetType(S"System.Decimal");
    myColumn->ReadOnly = true;
    myColumn->Expression = S"UnitPrice * Quantity";
    myColumn->Unique = false;
}

[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.

Requirements

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 | AllowDBNull | UniqueConstraint | Constraints

Show:
© 2014 Microsoft