Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DataColumn Constructor (String, Type, String, MappingType)

.NET Framework 1.1

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.

[Visual Basic]
Public Sub New( _
   ByVal columnName As String, _
   ByVal dataType As Type, _
   ByVal expr As String, _
   ByVal type As MappingType _
)
[C#]
public DataColumn(
   string columnName,
 Type dataType,
 string expr,
 MappingType type
);
[C++]
public: DataColumn(
   String* columnName,
 Type* dataType,
 String* expr,
 MappingType type
);
[JScript]
public function DataColumn(
   columnName : String,
 dataType : Type,
 expr : String,
 type : MappingType
);

Parameters

columnName
A string that represents the name of the column to be created. If set to a null reference (Nothing in Visual Basic) or an empty string (""), a default name will be given when added to the columns collection.
dataType
A supported DataType.
expr
The expression used to create this column. For more details, see the Expression property.
type
One of the MappingType values.

Exceptions

Exception Type Condition
ArgumentNullException No dataType was specified.

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 the column is named "fName," and the value it contains is "Bob," and type is set to MappingType.Attribute, the XML element would be:

<Name fName = 'Bob'/>

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

Example

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

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

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataColumn Class | DataColumn Members | System.Data Namespace | DataColumn Constructor Overload List | ColumnName | ColumnMapping | Caption | DefaultValue | DataTable | DataType

Show:
© 2015 Microsoft