This documentation is archived and is not being maintained.

DataColumnCollection.Add Method (String)

.NET Framework 1.1

Creates and adds a DataColumn object with the specified name to the DataColumnCollection.

[Visual Basic]
Overloads Public Overridable Function Add( _
   ByVal columnName As String _
) As DataColumn
public virtual DataColumn Add(
 string columnName
public: virtual DataColumn* Add(
 String* columnName
public function Add(
   columnName : String
) : DataColumn;


The name of the column.

Return Value

The newly created DataColumn.


Exception Type Condition
DuplicateNameException The collection already has a column with the specified name. (The comparison is not case-sensitive.)


By default, the DataType for the new column is string.

If a null reference (Nothing in Visual Basic) or an empty string ("") is passed in for the name, a default name ("Column1", "Column2", and so on) 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.


[Visual Basic] The following example creates and adds a new DataColumn to a DataColumnCollection of a DataTable.

[Visual Basic] 
Private Sub AddColumn()
    Dim cols As DataColumnCollection
    Dim myCol As DataColumn
    ' Get the DataColumnCollection from a table in a DataSet.
    cols = DataSet1.Tables("Prices").Columns
    myCol = cols.Add("Total")
    With myCol
       .DataType = System.Type.GetType("System.Decimal")
       .ReadOnly = True
       .Expression = "UnitPrice * Quantity"
       .Unique = False
    End With
 End Sub

[C#, C++, JScript] No example is available for C#, C++, or JScript. To view a Visual Basic example, click the Language Filter button Language Filter in the upper-left corner of the page.


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

DataColumnCollection Class | DataColumnCollection Members | System.Data Namespace | DataColumnCollection.Add Overload List | ColumnName | Contains | DataType | Expression