Edit

Share via


DataColumnCollection.Add Method

Definition

Creates and adds a DataColumn object to the DataColumnCollection.

Overloads

Add()

Creates and adds a DataColumn object to the DataColumnCollection.

Add(DataColumn)

Creates and adds the specified DataColumn object to the DataColumnCollection.

Add(String)

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

Add(String, Type)

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

Add(String, Type, String)

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

Add()

Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs

Creates and adds a DataColumn object to the DataColumnCollection.

public System.Data.DataColumn Add();
public virtual System.Data.DataColumn Add();

Returns

The newly created DataColumn.

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(DataColumn)

Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs

Creates and adds the specified DataColumn object to the DataColumnCollection.

public void Add(System.Data.DataColumn column);

Parameters

column
DataColumn

The DataColumn to add.

Exceptions

The column parameter is null.

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(String)

Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs

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

public System.Data.DataColumn Add(string? columnName);
public System.Data.DataColumn Add(string columnName);
public virtual System.Data.DataColumn Add(string columnName);

Parameters

columnName
String

The name of the column.

Returns

The newly created DataColumn.

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(String, Type)

Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs

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

public System.Data.DataColumn Add(string? columnName, Type type);
public System.Data.DataColumn Add(string columnName, Type type);
public virtual System.Data.DataColumn Add(string columnName, Type type);

Parameters

columnName
String

The ColumnName to use when you create the column.

type
Type

The DataType of the new column.

Returns

The newly created DataColumn.

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(String, Type, String)

Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs

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

public System.Data.DataColumn Add(string? columnName, Type type, string expression);
public System.Data.DataColumn Add(string columnName, Type type, string expression);
public virtual System.Data.DataColumn Add(string columnName, Type type, string expression);

Parameters

columnName
String

The name to use when you create the column.

type
Type

The DataType of the new column.

expression
String

The expression to assign to the Expression property.

Returns

The newly created DataColumn.

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

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1