Creates and adds a DataColumn with the specified name, type, and compute expression to the columns collection.
[Visual Basic] Overloads Public Function Add( _ ByVal columnName As String, _ ByVal type As Type, _ ByVal expression As String _ ) As DataColumn [C#] public DataColumn Add( string columnName, Type type, string expression ); [C++] public: DataColumn* Add( String* columnName, Type* type, String* expression ); [JScript] public function Add( columnName : String, type : Type, expression : String ) : DataColumn;
The newly created DataColumn.
Exception Type | Condition |
---|---|
DuplicateNameException | The collection already has a column with the same name (the comparison is not case-sensitive). |
InvalidExpressionException | The expression can't be bound. |
If a null reference (in Visual Basic Nothing) or an empty string ("") is passed in for the name, a default name ("Column1", "Column2", etc.) is given to the column.
The Contains method determine whether a column with the proposed name already exists in the collection.
If the collection is succesfully changed by adding or removing columns, the OnCollectionChanged event occurs.
The following example creates and adds a new DataColumn to a ColumnsCollection of a DataTable.
[Visual Basic]
Private Sub AddColumn() Dim cols As ColumnsCollection Dim myCol As DataColumn ' Get the ColumnsCollection of a table in a DataSet. cols = DataSet1.Tables("Orders").Columns ' Add a new column and return it. myCol = cols.Add("Total", System.Type.GetType("System.Currency"), _ "Price + Tax") myCol.ReadOnly = True myCol.Unique = False End Sub
ColumnsCollection Class | ColumnsCollection Members | System.Data Namespace | ColumnsCollection.Add Overload List | ColumnName | Contains | DataType | Expression