System.Data Namespace DataColumn Class
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.
[ VB ]
Overloads 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
);
- columnName
- A string that represents the name of the column to be created. If set to a null reference 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.
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.
The following example constructs a computed column.
private void CreateComputedColumn ( ) {
System.Type dtype = new System.Type.GetType ( "System.Decimal" );
// the expression multiplies the "Price" column value
// by the "Quantity" to create the "Total" column.
string strExpr = "Price * Quantity";
// create the column, setting the type to Attribute.
DataColumn myColumn = new DataColumn ( "Total", dtype,
strExpr, MappingType.Attribute );
// set various properties.
myColumn.Unique = true;
myColumn.AutoIncrement = false;
myColumn.ReadOnly = true;
// add the column to a DataTable object's to ColumnsCollection.
myDataSet.Tables [ "OrderDetails" ].Columns.Add ( myColumn );
}
Private Sub CreateComputedColumn ( )
Dim dtype As New System.Type.GetType ( "System.Decimal" )
' the expression multiplies the "Price" column value
' by the "Quantity" to create the "Total" column.
Dim strExpr As String = "Price * Quantity"
' create the column, setting the type to Attribute.
Dim myColumn As New DataColumn ( "Total", dtype, _
strExpr, MappingType.Attribute )
' set various properties.
myColumn.Unique = True
myColumn.AutoIncrement = False
myColumn.ReadOnly = True
' add the column to a DataTable object's ColumnsCollection.
myDataSet.Tables ( "OrderDetails" ).Columns.Add ( myColumn )
End Sub |
|
C# |
VB |
DataColumn Members DataColumn Constructor Overload List ColumnName ColumnMapping Caption DefaultValue DataTable DataType