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

定義

建立 DataColumn 物件,並將它加入 DataColumnCollectionCreates and adds a DataColumn object to the DataColumnCollection.

多載

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

建立 DataColumn 物件,並將它加入 DataColumnCollectionCreates and adds a DataColumn object to the DataColumnCollection.

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

建立指定的 DataColumn 物件,並將它加入 DataColumnCollectionCreates and adds the specified DataColumn object to the DataColumnCollection.

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

建立具有指定名稱的 DataColumn 物件,並將它加入 DataColumnCollectionCreates and adds a DataColumn object that has the specified name to the DataColumnCollection.

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

建立具有指定名稱和型別的 DataColumn 物件,並將它加入 DataColumnCollectionCreates 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)

建立具有指定名稱、型別和運算式的 DataColumn 物件,並將它加入 DataColumnCollectionCreates and adds a DataColumn object that has the specified name, type, and expression to the DataColumnCollection.

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

建立 DataColumn 物件,並將它加入 DataColumnCollectionCreates 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

傳回

新建立的 DataColumnThe newly created DataColumn.

範例

下列範例會建立新的,並DataColumn將其DataColumnCollection新增至DataTable的。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

備註

預設名稱("Column1"、"Column2" 等等)會提供給資料行。A default name ("Column1", "Column2", and so on) is given to the column.

如果藉由加入或移除資料行成功變更集合,則CollectionChanged會發生此事件。If the collection is successfully changed by adding or removing columns, the CollectionChanged event occurs.

另請參閱

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

建立指定的 DataColumn 物件,並將它加入 DataColumnCollectionCreates 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)

參數

column
DataColumn DataColumn DataColumn DataColumn

要加入的 DataColumnThe DataColumn to add.

例外狀況

column 參數為 nullThe 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. 請參閱 Expression 屬性,取得如何建立運算式的詳細資訊。See the Expression property for more information about how to create expressions.

範例

下列範例會將加入DataColumn DataColumnCollection至。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

備註

如果藉由加入或移除資料行成功變更集合,則CollectionChanged會發生此事件。If the collection is successfully changed by adding or removing columns, the CollectionChanged event occurs.

另請參閱

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

建立具有指定名稱的 DataColumn 物件,並將它加入 DataColumnCollectionCreates 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

參數

columnName
String String String String

資料行名稱。The name of the column.

傳回

新建立的 DataColumnThe newly created DataColumn.

例外狀況

集合不具有含有指定名稱的資料行The collection already has a column with the specified name. (這項比較不會區分大小寫。)(The comparison is not case-sensitive.)

範例

下列範例會建立新DataColumn的,並將其加入DataColumnCollectionDataTable的。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

備註

根據預設,新DataType資料行的是。 stringBy default, the DataType for the new column is string.

如果null針對名稱傳入或空字串(""),則會將預設名稱("Column1"、"Column2" 等等)提供給資料行。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.

Contains使用方法來判斷具有提議名稱的資料行是否已存在於集合中。Use the Contains method to determine whether a column with the proposed name already exists in the collection.

如果藉由加入或移除資料行成功變更集合,則CollectionChanged會發生此事件。If the collection is successfully changed by adding or removing columns, the CollectionChanged event occurs.

另請參閱

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

建立具有指定名稱和型別的 DataColumn 物件,並將它加入 DataColumnCollectionCreates 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

參數

columnName
String String String String

建立資料行時要使用的 ColumnNameThe ColumnName to use when you create the column.

type
Type Type Type Type

新資料行的 DataTypeThe DataType of the new column.

傳回

新建立的 DataColumnThe newly created DataColumn.

例外狀況

集合不具有含有指定名稱的資料行The collection already has a column with the specified name. (這項比較不會區分大小寫。)(The comparison is not case-sensitive.)

運算式無效。The expression is invalid. 請參閱 Expression 屬性,取得如何建立運算式的詳細資訊。See the Expression property for more information about how to create expressions.

範例

下列範例會建立新DataColumn的,並將其加入DataColumnCollectionDataTable的。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

備註

如果null針對名稱傳入或空字串(""),則會將預設名稱("Column1"、"Column2" 等等)提供給資料行。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.

Contains使用方法來判斷具有提議名稱的資料行是否已存在於集合中。Use the Contains method to determine whether a column with the proposed name already exists in the collection.

如果藉由加入或移除資料行成功變更集合,則CollectionChanged會發生此事件。If the collection is successfully changed by adding or removing columns, the CollectionChanged event occurs.

另請參閱

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

建立具有指定名稱、型別和運算式的 DataColumn 物件,並將它加入 DataColumnCollectionCreates 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

參數

columnName
String String String String

建立資料行時要使用的名稱。The name to use when you create the column.

type
Type Type Type Type

新資料行的 DataTypeThe DataType of the new column.

expression
String String String String

要指派給 Expression 屬性的運算式。The expression to assign to the Expression property.

傳回

新建立的 DataColumnThe newly created DataColumn.

例外狀況

集合不具有含有指定名稱的資料行The collection already has a column with the specified name. (這項比較不會區分大小寫。)(The comparison is not case-sensitive.)

運算式無效。The expression is invalid. 請參閱 Expression 屬性,取得如何建立運算式的詳細資訊。See the Expression property for more information about how to create expressions.

範例

下列範例會建立新DataColumn的,並將其加入DataColumnCollectionDataTable的。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

備註

如果null針對名稱傳入或空字串(""),則會將預設名稱("Column1"、"Column2" 等等)提供給資料行。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.

Contains使用方法來判斷具有提議名稱的資料行是否已存在於集合中。Use the Contains method to determine whether a column with the proposed name already exists in the collection.

如果藉由加入或移除資料行成功變更集合,則CollectionChanged會發生此事件。If the collection is successfully changed by adding or removing columns, the CollectionChanged event occurs.

另請參閱

適用於