Export (0) Print
Expand All
3 out of 6 rated this helpful - Rate this topic

DataColumn.DataType Property

Gets or sets the type of data stored in the column.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)
public Type DataType { get; set; }

Property Value

Type: System.Type
A Type object that represents the column data type.
ExceptionCondition
ArgumentException

The column already has data stored.

Setting the DataType value is very important to guaranteeing the correct creation and updating of data in a data source.

The DataType property supports the following base .NET Framework data types:

as well as the following array type:

  • Byte[]

An exception is generated when changing this property after the column has begun storing data.

If AutoIncrement is set to true before setting the DataType property, and you try to set the type to anything except an integer type, an exception is generated.

NoteNote

A column of data type Byte[] requires special treatment in certain cases since, unlike the base .NET Framework data types, it is a reference data type. If a column of data type Byte[] is used as a PrimaryKey, or as a Sort or RowFilter key for a DataView, any change to the column value must involve assigning the Byte[] column value to a separately instantiated Byte[] object. This assignment is required to trigger the update of the internal indexes used by sorting, filtering, and primary key operations. This is illustrated by the following example:

byte[] columnValue = (byte[])myDataTable.Rows[0][0];
byte[] newValue = (byte[])columnValue.Clone();
newValue[1] = 2;
myDataTable.Rows[0][0] = newValue;
NoteNote

Although it is possible to define a column as data type other than the base .NET Framework data types and Byte[], such a column will be treated as a user-defined type, subject to the following usage restrictions. (For more information on user-defined types, see Creating and Using User-Defined Types.)

  • The column cannot be part of a RowFilter or Select expression.

  • If the column is used as a PrimaryKey, or as a Sort or for a DataView, it must be treated as an immutable field; the column data must not be changed once it has been added to the table.

  • Its ColumnMapping can be only set to MappingType.Element.

  • The class that implements the column’s data type must be must be marked with the SerializableAttribute, and if necessary implement the ISerializable or IXmlSerializable interface.

  • Support for change tracking is limited. To utilize the DataTable class’s change tracking mechanism, the class that implements the column’s data type must either implement the IChangeTracking interface, or take over the responsibility for informing the DataRow when the column value has been modified, either by calling SetModified on the row or by assigning the column value object to a separately instantiated column value object.

The following example adds columns of several data types to a DataTable, and then adds one row to the table.

public DataTable MakeDataTable(){

    DataTable myTable;
    DataRow myNewRow; 
    // Create a new DataTable.
    myTable = new DataTable("My Table");

    // Create DataColumn objects of data types.
    DataColumn colString = new DataColumn("StringCol");
    colString.DataType = System.Type.GetType("System.String");
    myTable.Columns.Add(colString); 

    DataColumn colInt32 = new DataColumn("Int32Col");
    colInt32.DataType = System.Type.GetType("System.Int32");
    myTable.Columns.Add(colInt32);

    DataColumn colBoolean = new DataColumn("BooleanCol");
    colBoolean.DataType = System.Type.GetType("System.Boolean");
    myTable.Columns.Add(colBoolean);

    DataColumn colTimeSpan = new DataColumn("TimeSpanCol");
    colTimeSpan.DataType = System.Type.GetType("System.TimeSpan");
    myTable.Columns.Add(colTimeSpan);

    DataColumn colDateTime = new DataColumn("DateTimeCol");
    colDateTime.DataType = System.Type.GetType("System.DateTime");
    myTable.Columns.Add(colDateTime);

    DataColumn colDecimal = new DataColumn("DecimalCol");
    colDecimal.DataType = System.Type.GetType("System.Decimal");
    myTable.Columns.Add(colDecimal);

    DataColumn colByteArray = new DataColumn("ByteArrayCol");
    colByteArray.DataType = System.Type.GetType("System.Byte[]");
    myTable.Columns.Add(colByteArray);


    // Populate one row with values.
    myNewRow = myTable.NewRow();

    myNewRow["StringCol"] = "Item Name";
    myNewRow["Int32Col"] = 2147483647;
    myNewRow["BooleanCol"] = true;
    myNewRow["TimeSpanCol"] = new TimeSpan(10,22,10,15,100);
    myNewRow["DateTimeCol"] = System.DateTime.Today;
    myNewRow["DecimalCol"] = 64.0021;
    myNewRow["ByteArrayCol"] = new Byte[] { 1, 5, 120 };
    myTable.Rows.Add(myNewRow);
    return myTable;  
 }

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.