Export (0) Print
Expand All

DataColumn Constructor

.NET Framework 1.1

Initializes a new instance of the DataColumn class.

Overload List

Initializes a new instance of a DataColumn class.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New()
[C#] public DataColumn();
[C++] public: DataColumn();
[JScript] public function DataColumn();

Inititalizes a new instance of the DataColumn class using the specified column name.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String)
[C#] public DataColumn(string);
[C++] public: DataColumn(String*);
[JScript] public function DataColumn(String);

Inititalizes a new instance of the DataColumn class using the specified column name and data type.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, Type)
[C#] public DataColumn(string, Type);
[C++] public: DataColumn(String*, Type*);
[JScript] public function DataColumn(String, Type);

Initializes a new instance of the DataColumn class using the specified name, data type, and expression.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, Type, String)
[C#] public DataColumn(string, Type, string);
[C++] public: DataColumn(String*, Type*, String*);
[JScript] public function DataColumn(String, Type, String);

Initializes a new instance of the DataColumn class using the specified name, data type, expression, and value that determines whether the column is an attribute.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, Type, String, MappingType)
[C#] public DataColumn(string, Type, string, MappingType);
[C++] public: DataColumn(String*, Type*, String*, MappingType);
[JScript] public function DataColumn(String, Type, String, MappingType);

Example

[Visual Basic, C#, C++] The following example constructs a computed column.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the DataColumn constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Private Sub CreateComputedColumn(ByVal myTable As DataTable)
    Dim myColumn As DataColumn
    Dim dt As System.Type
    Dim strExpr As String
    dt = System.Type.GetType("System.Decimal")
    ' The expression multiplies the "Price" column value by the "Quantity" to create the "Total" column.
    strExpr = "Price * Quantity"
    ' Create the column, setting the type to Attribute.
    myColumn = New DataColumn("Total", dt, strExpr, MappingType.Attribute)
    ' Set various properties.
    myColumn.AutoIncrement = False
    myColumn.ReadOnly = True
    ' Add the column to a DataTable object's DataColumnCollection.
    DataSet1.Tables("OrderDetails").Columns.Add(myColumn)
 End Sub

[C#] 
private void CreateComputedColumn(DataTable myTable){
    System.Type myDataType;
    myDataType = System.Type.GetType("System.Decimal");
    string strExpr;
    // The expression multiplies the "Price" column value by the "Quantity" to create the "Total" column.
    strExpr = "Price * Quantity";
    // Create the column, setting the type to Attribute.
    DataColumn myColumn = new DataColumn("Total", myDataType, 
    strExpr, MappingType.Attribute);
    // Set various properties.
    myColumn.AutoIncrement = false;
    myColumn.ReadOnly = true;
    // Add the column to a DataTable object's to DataColumnCollection.
    DataSet1.Tables["OrderDetails"].Columns.Add(myColumn);
 }

[C++] 
private:
void CreateComputedColumn(DataTable* /*myTable*/){
    System::Type* myDataType;
    myDataType = System::Type::GetType(S"System.Decimal");
    String* strExpr;
    // The expression multiplies the "Price" column value by the "Quantity" to create the "Total" column.
    strExpr = S"Price * Quantity";
    // Create the column, setting the type to Attribute.
    DataColumn* myColumn = new DataColumn(S"Total", myDataType, 
    strExpr, MappingType::Attribute);
    // Set various properties.
    myColumn->AutoIncrement = false;
    myColumn->ReadOnly = true;
    // Add the column to a DataTable object's to DataColumnCollection.
    DataSet1->Tables->Item[S"OrderDetails"]->Columns->Add(myColumn);
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

DataColumn Class | DataColumn Members | System.Data Namespace

Show:
© 2014 Microsoft