System.Data Namespace DataColumnCollection Class
Initializes and adds a DataColumn with the specified name to the columns collection.
[ VB ]
Overloads Public Function Add ( _
ByVal columnName As String _
) As DataColumn
[ C# ]
public virtual DataColumn Add (
string columnName
);
[ C++ ]
public: virtual DataColumn* Add (
String* columnName
);
[ JScript ]
public function Add (
columnName : String
) : DataColumn;
- columnName
- The name of the column.
The newly created DataColumn.
Exception Type |
Condition |
DuplicateNameException |
The collection already has a column with the same name ( the comparison is not case-sensitive ). |
By default, the column's DataType is string.
If a null reference or an empty string ( "" ) is passed in for the name, a default name ( "Column1", "Column2", etc. ) is given to the column.
Use the Contains method to determine whether a column with the proposed name already exists in the collection.
If the collection is successfully changed by adding or removing columns, the CollectionChanged event occurs.
The following example initializes and adds a new DataColumn to the DataColumnCollection of a DataTable using this constructor.
Private Sub AddColumn ( )
' get the DataColumnCollection from a table in a DataSet.
Dim cols As DataColumnCollection = ds.Tables ( "Prices" ).Columns
Dim myCol As DataColumn = cols.Add ( "Total" )
With myCol
.DataType = System.Type.GetType ( "System.Decimal" )
.Expression = "UnitPrice * Quantity"
.ReadOnly = True
.Unique = False
End With
End Sub
DataColumnCollection Members DataColumnCollection.Add Overload List ColumnName