DataColumn Constructors

Definition

Initializes a new instance of the DataColumn class.

Overloads

DataColumn()

Initializes a new instance of the DataColumn class as type string.

DataColumn(String)

Initializes a new instance of the DataColumn class, as type string, using the specified column name.

DataColumn(String, Type)

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

DataColumn(String, Type, String)

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

DataColumn(String, Type, String, MappingType)

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.

DataColumn()

Source:
DataColumn.cs
Source:
DataColumn.cs
Source:
DataColumn.cs

Initializes a new instance of the DataColumn class as type string.

C#
public DataColumn();

Examples

The following example creates a new DataColumn, sets various properties, and adds it to a DataColumnCollection for the DataTable object.

C#
private void AddDataColumn(DataTable table)
{
    DataColumn column = new DataColumn();

    // Set various properties.
    column.ColumnName = "id";
    column.DataType = System.Type.GetType("System.Int32");
    column.AutoIncrement = true;
    column.AutoIncrementSeed = 1;
    column.AutoIncrementStep = 1;
    column.ReadOnly = true;

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

Remarks

When created, a DataColumn object has no default ColumnName or Caption. When you add it to a DataColumnCollection, a default name ("Column1", "Column2", and so on) will be generated if a name has not been assigned to the ColumnName.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DataColumn(String)

Source:
DataColumn.cs
Source:
DataColumn.cs
Source:
DataColumn.cs

Initializes a new instance of the DataColumn class, as type string, using the specified column name.

C#
public DataColumn(string? columnName);
C#
public DataColumn(string columnName);

Parameters

columnName
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.

Examples

The following example creates a new DataColumn with a specified ColumnName.

C#
private void AddDataColumn(DataTable table)
{
    DataColumn column = new DataColumn("id");

    // Set various properties.
    column.DataType = System.Type.GetType("System.Int32");
    column.AutoIncrement = true;
    column.AutoIncrementSeed = 1;
    column.AutoIncrementStep = 1;
    column.ReadOnly = true;

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

Remarks

By default, the name specific to a column becomes the Caption property value.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DataColumn(String, Type)

Source:
DataColumn.cs
Source:
DataColumn.cs
Source:
DataColumn.cs

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

C#
public DataColumn(string? columnName, Type dataType);
C#
public DataColumn(string columnName, Type dataType);

Parameters

columnName
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

A supported DataType.

Exceptions

No dataType was specified.

Examples

The following example creates a new DataColumn with a specified ColumnName and DataType.

C#
private void AddDataColumn(DataTable table)
{
    System.Type typeInt32 =
        System.Type.GetType("System.Int32");
    DataColumn column = new DataColumn("id", typeInt32);

    // Set various properties.
    column.AutoIncrement = true;
    column.AutoIncrementSeed = 1;
    column.AutoIncrementStep = 1;
    column.ReadOnly = true;

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

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DataColumn(String, Type, String)

Source:
DataColumn.cs
Source:
DataColumn.cs
Source:
DataColumn.cs

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

C#
public DataColumn(string? columnName, Type dataType, string? expr);
C#
public DataColumn(string columnName, Type dataType, string expr);

Parameters

columnName
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

A supported DataType.

expr
String

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

Exceptions

No dataType was specified.

Examples

The following example creates a computed column.

C#
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);
}

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DataColumn(String, Type, String, MappingType)

Source:
DataColumn.cs
Source:
DataColumn.cs
Source:
DataColumn.cs

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.

C#
public DataColumn(string? columnName, Type dataType, string? expr, System.Data.MappingType type);
C#
public DataColumn(string columnName, Type dataType, string expr, System.Data.MappingType type);

Parameters

columnName
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

A supported DataType.

expr
String

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

type
MappingType

One of the MappingType values.

Exceptions

No dataType was specified.

Examples

The following example constructs a computed column.

C#
private void CreateComputedColumn(DataTable table)
{
    System.Type myDataType =
        System.Type.GetType("System.Decimal");

    // The expression multiplies the "Price" column value
    // by the "Quantity" to create the "Total" column.
    string expression = "Price * Quantity";

    // Create the column, setting the type to Attribute.
    DataColumn column = new DataColumn("Total", myDataType,
        expression, MappingType.Attribute);

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

    // Add the column to a DataTable object's to DataColumnCollection.
    DataSet1.Tables["OrderDetails"].Columns.Add(column);
}

Remarks

The type argument sets the ColumnMapping property. The property specifies how a DataColumn is mapped when a DataSet is transformed into an XML document. For example, if the column is named "fName," and the value it contains is "Bob," and type is set to MappingType.Attribute, the XML element would be as follows:

<Name fName = 'Bob'/>

For more information about how columns are mapped to elements or attributes, see the ColumnMapping property.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1