Upraviť

Zdieľať cez


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.

public:
 DataColumn();
public DataColumn ();
Public Sub New ()

Examples

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

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);
}
Private Sub AddDataColumn(ByVal table As DataTable)
    Dim column As New DataColumn()

    ' Set various properties.
    With column
       .ColumnName = "id"
       .DataType = System.Type.GetType("System.Int32")
       .AutoIncrement = True
       .AutoIncrementSeed = 1
       .AutoIncrementStep = 1
       .ReadOnly = True
     End With

    ' Add to Columns collection.
    table.Columns.Add(column)
End Sub

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

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.

public:
 DataColumn(System::String ^ columnName);
public DataColumn (string? columnName);
public DataColumn (string columnName);
new System.Data.DataColumn : string -> System.Data.DataColumn
Public Sub New (columnName As String)

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.

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

Private Sub AddDataColumn(ByVal table As DataTable)
    Dim column As DataColumn 
    column = New DataColumn("id")

    ' Set various properties.
    With column
       .DataType = System.Type.GetType("System.Int32")
       .AutoIncrement = True
       .AutoIncrementSeed = 1
       .AutoIncrementStep = 1
       .ReadOnly = True
     End With

    ' Add to Columns collection.
    table.Columns.Add(column)
End Sub

Remarks

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

See also

Applies to

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.

public:
 DataColumn(System::String ^ columnName, Type ^ dataType);
public DataColumn (string? columnName, Type dataType);
public DataColumn (string columnName, Type dataType);
new System.Data.DataColumn : string * Type -> System.Data.DataColumn
Public Sub New (columnName As String, dataType As 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.

Exceptions

No dataType was specified.

Examples

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

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

Private Sub AddDataColumn(ByVal table As DataTable)
    
   Dim typeInt32 As System.Type = _
      System.Type.GetType("System.Int32")
   Dim column As DataColumn = _
       New DataColumn("id", typeInt32)

   ' Set various properties.
   With column
      .AutoIncrement = True
      .AutoIncrementSeed = 1
      .AutoIncrementStep = 1
      .ReadOnly = True
    End With

   ' Add to Columns collection.
   table.Columns.Add(column)
End Sub

See also

Applies to

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.

public:
 DataColumn(System::String ^ columnName, Type ^ dataType, System::String ^ expr);
public DataColumn (string? columnName, Type dataType, string? expr);
public DataColumn (string columnName, Type dataType, string expr);
new System.Data.DataColumn : string * Type * string -> System.Data.DataColumn
Public Sub New (columnName As String, dataType As Type, expr As String)

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.

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);
}
Private Sub AddDataColumn(ByVal table As DataTable)
    Dim column As DataColumn 
    Dim decimalType As System.Type
 
    decimalType = System.Type.GetType("System.Decimal")
    column = New DataColumn("Tax", decimalType, "UnitPrice * .0862")

    ' Set various properties.
    With column
       .AutoIncrement = False
       .ReadOnly = True
    End With

    ' Add to Columns collection.
    table.Columns.Add(column)
 End Sub

See also

Applies to

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.

public:
 DataColumn(System::String ^ columnName, Type ^ dataType, System::String ^ expr, System::Data::MappingType type);
public DataColumn (string? columnName, Type dataType, string? expr, System.Data.MappingType type);
public DataColumn (string columnName, Type dataType, string expr, System.Data.MappingType type);
new System.Data.DataColumn : string * Type * string * System.Data.MappingType -> System.Data.DataColumn
Public Sub New (columnName As String, dataType As Type, expr As String, type As MappingType)

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.

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);
}
Private Sub CreateComputedColumn(ByVal table As DataTable)
    Dim column As DataColumn
    Dim decimalType As System.Type = _
        System.Type.GetType("System.Decimal")

    ' The expression multiplies the "Price" column value by the 
    ' "Quantity" to create the "Total" column.
    Dim expression As String = "Price * Quantity"

    ' Create the column, setting the type to Attribute.
    column = New DataColumn("Total", decimalType, _
        expression, MappingType.Attribute)

    ' Set various properties.
    column.AutoIncrement = False
    column.ReadOnly = True

    ' Add the column to a DataTable object's DataColumnCollection.
    DataSet1.Tables("OrderDetails").Columns.Add(column)
End Sub

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