DataColumnCollection.Add DataColumnCollection.Add DataColumnCollection.Add DataColumnCollection.Add Method

Definition

Creates and adds a DataColumn object to the DataColumnCollection.

Overloads

Add() Add() Add() Add()

Creates and adds a DataColumn object to the DataColumnCollection.

Add(DataColumn) Add(DataColumn) Add(DataColumn) Add(DataColumn)

Creates and adds the specified DataColumn object to the DataColumnCollection.

Add(String) Add(String) Add(String) Add(String)

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

Add(String, Type) Add(String, Type) Add(String, Type)

Creates and adds a DataColumn object that has the specified name and type to the DataColumnCollection.

Add(String, Type, String) Add(String, Type, String) Add(String, Type, String)

Creates and adds a DataColumn object that has the specified name, type, and expression to the DataColumnCollection.

Add() Add() Add() Add()

Creates and adds a DataColumn object to the DataColumnCollection.

public:
 virtual System::Data::DataColumn ^ Add();
public virtual System.Data.DataColumn Add ();
abstract member Add : unit -> System.Data.DataColumn
override this.Add : unit -> System.Data.DataColumn
Public Overridable Function Add () As DataColumn

Returns

Examples

The following example creates and adds a new DataColumn to the DataColumnCollection of a DataTable.

Private Sub AddColumn()
    ' Get the DataColumnCollection from a table in a DataSet.
    Dim columns As DataColumnCollection = _
        DataSet1.Tables("Prices").Columns
    Dim column As DataColumn = columns.Add()

    With column
       .DataType = System.Type.GetType("System.Decimal")
       .ColumnName = "Total"
       .Expression = "UnitPrice * Quantity"
       .ReadOnly = True
       .Unique = False
    End With
End Sub

Remarks

A default name ("Column1", "Column2", and so on) is given to the column.

If the collection is successfully changed by adding or removing columns, the CollectionChanged event occurs.

See also

Add(DataColumn) Add(DataColumn) Add(DataColumn) Add(DataColumn)

Creates and adds the specified DataColumn object to the DataColumnCollection.

public:
 void Add(System::Data::DataColumn ^ column);
public void Add (System.Data.DataColumn column);
member this.Add : System.Data.DataColumn -> unit
Public Sub Add (column As DataColumn)

Parameters

Exceptions

The column already belongs to this collection, or to another collection.

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

The expression is invalid. See the Expression property for more information about how to create expressions.

Examples

The following example adds a DataColumn to a DataColumnCollection.

Private Sub AddDataColumn()
    ' Get the DataColumnCollection from a DataTable in a DataSet.
    Dim columns As DataColumnCollection = _
        DataSet1.Tables("Orders").Columns
 
    Dim column As New DataColumn()
    With column
       .DataType = System.Type.GetType("System.Decimal")
       .ColumnName = "ItemPrice"
       .Caption = "Price"
       .ReadOnly = False
       .Unique = False
       .DefaultValue = 0
    End With
    columns.Add(column)
End Sub

Remarks

If the collection is successfully changed by adding or removing columns, the CollectionChanged event occurs.

See also

Add(String) Add(String) Add(String) Add(String)

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

public:
 virtual System::Data::DataColumn ^ Add(System::String ^ columnName);
public virtual System.Data.DataColumn Add (string columnName);
abstract member Add : string -> System.Data.DataColumn
override this.Add : string -> System.Data.DataColumn
Public Overridable Function Add (columnName As String) As DataColumn

Parameters

columnName
String String String String

The name of the column.

Returns

Exceptions

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

Examples

The following example creates and adds a new DataColumn to a DataColumnCollection of a DataTable.

Private Sub AddColumn()
    ' Get the DataColumnCollection from a table in a DataSet.
    Dim columns As DataColumnCollection = _
        DataSet1.Tables("Prices").Columns
    Dim column As DataColumn = columns.Add("Total")

    With column
       .DataType = System.Type.GetType("System.Decimal")
       .ReadOnly = True
       .Expression = "UnitPrice * Quantity"
       .Unique = False
    End With
End Sub

Remarks

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

If null 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.

See also

Add(String, Type) Add(String, Type) Add(String, Type)

Creates and adds a DataColumn object that has the specified name and type to the DataColumnCollection.

public:
 virtual System::Data::DataColumn ^ Add(System::String ^ columnName, Type ^ type);
public virtual System.Data.DataColumn Add (string columnName, Type type);
abstract member Add : string * Type -> System.Data.DataColumn
override this.Add : string * Type -> System.Data.DataColumn

Parameters

columnName
String String String String

The ColumnName to use when you create the column.

type
Type Type Type Type

The DataType of the new column.

Returns

Exceptions

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

The expression is invalid. See the Expression property for more information about how to create expressions.

Examples

The following example creates and adds a new DataColumn to a DataColumnCollection of a DataTable.

Private Sub AddColumn()
    Dim columns As DataColumnCollection = _
        DataSet1.Tables("Orders").Columns
 
    ' Add a new column and return it.
    Dim column As DataColumn = columns.Add( _
        "Total", System.Type.GetType("System.Decimal"))
    column.ReadOnly = True
    column.Unique = False
End Sub

Remarks

If null 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.

See also

Add(String, Type, String) Add(String, Type, String) Add(String, Type, String)

Creates and adds a DataColumn object that has the specified name, type, and expression to the DataColumnCollection.

public:
 virtual System::Data::DataColumn ^ Add(System::String ^ columnName, Type ^ type, System::String ^ expression);
public virtual System.Data.DataColumn Add (string columnName, Type type, string expression);
abstract member Add : string * Type * string -> System.Data.DataColumn
override this.Add : string * Type * string -> System.Data.DataColumn

Parameters

columnName
String String String String

The name to use when you create the column.

type
Type Type Type Type

The DataType of the new column.

expression
String String String String

The expression to assign to the Expression property.

Returns

Exceptions

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

The expression is invalid. See the Expression property for more information about how to create expressions.

Examples

The following example creates and adds a new DataColumn to a DataColumnCollection of a DataTable.

Private Sub AddColumn()
    ' Get the DataColumnCollection of a table in a DataSet.
    Dim columns As DataColumnCollection = _
        DataSet1.Tables("Orders").Columns

    ' Add a new column and return it.
    Dim column As DataColumn = _
        columns.Add("Total", System.Type.GetType( _
        "System.Decimal"), "Price + Tax")
    column.ReadOnly = True
    column.Unique = False
 End Sub

Remarks

If null 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.

See also

Applies to