Redigera

Dela 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();
public System.Data.DataColumn Add ();
public virtual System.Data.DataColumn Add ();
member this.Add : unit -> System.Data.DataColumn
abstract member Add : unit -> System.Data.DataColumn
override this.Add : unit -> System.Data.DataColumn
Public Function Add () As DataColumn
Public Overridable Function Add () As DataColumn

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

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);
public void Add (System.Data.DataColumn column);
member this.Add : System.Data.DataColumn -> unit
Public Sub Add (column As DataColumn)

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

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(System::String ^ columnName);
public:
 virtual System::Data::DataColumn ^ Add(System::String ^ columnName);
public System.Data.DataColumn Add (string? columnName);
public System.Data.DataColumn Add (string columnName);
public virtual System.Data.DataColumn Add (string columnName);
member this.Add : string -> System.Data.DataColumn
abstract member Add : string -> System.Data.DataColumn
override this.Add : string -> System.Data.DataColumn
Public Function Add (columnName As String) As DataColumn
Public Overridable Function Add (columnName As String) As DataColumn

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

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(System::String ^ columnName, Type ^ type);
public:
 virtual System::Data::DataColumn ^ Add(System::String ^ columnName, Type ^ type);
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);
member this.Add : string * Type -> System.Data.DataColumn
abstract member Add : string * Type -> System.Data.DataColumn
override this.Add : string * Type -> System.Data.DataColumn
Public Function Add (columnName As String, type As Type) As DataColumn
Public Overridable Function Add (columnName As String, type As Type) As DataColumn

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

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(System::String ^ columnName, Type ^ type, System::String ^ expression);
public:
 virtual System::Data::DataColumn ^ Add(System::String ^ columnName, Type ^ type, System::String ^ expression);
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);
member this.Add : string * Type * string -> System.Data.DataColumn
abstract member Add : string * Type * string -> System.Data.DataColumn
override this.Add : string * Type * string -> System.Data.DataColumn
Public Function Add (columnName As String, type As Type, expression As String) As DataColumn
Public Overridable Function Add (columnName As String, type As Type, expression As String) As DataColumn

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