Export (0) Print
Expand All

DataColumn Constructor (String, Type, String)

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

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

public DataColumn(
	string columnName,
	Type dataType,
	string expr
)

Parameters

columnName
Type: System.String

A string that represents the name of the column to be created. If set to null or an empty string (""), a default name will be specified when added to the columns collection.

dataType
Type: System.Type

A supported DataType.

expr
Type: System.String

The expression used to create this column. For more information, see the Expression property.

ExceptionCondition
ArgumentNullException

No dataType was specified.

The following example creates a computed column.

private void AddDataColumn(DataTable table)
{
    System.Type decimalType;
    decimalType = System.Type.GetType("System.Decimal");

    // Create the column. The name is 'Tax,' with data type Decimal,and  
    // an expression ('UnitPrice * .0862) to calculate the tax.
    DataColumn column = new DataColumn("Tax", 
        decimalType, "UnitPrice * .0862");

    // Set various properties.
    column.AutoIncrement = false;
    column.ReadOnly = true;

    // Add to Columns collection.;
    table.Columns.Add(column);
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show:
© 2015 Microsoft