The DataColumn represents a column schema within a table. The DataTable stores the DataColumn objects in its DataTable.Columns collection. The DataColumn object doesn’t contain data. Instead, it defines the properties of a column such as the .NET data type (DataType), whether it allows null values (AllowDBNull), whether it is read-only (ReadOnly), whether values in each row of the column must be unique (Unique), the column name (ColumnName), the default value when a new row is created (DefaultValue), the maximum length for text fields (MaxLength), and the position relative to other columns in the table (Ordinal). If you retrieve data without schema information, some of these values (such as MaxLength) aren’t initialized. However, even if you do retrieve schema information, you’ll find that some information, such as DefaultValue, is never retrieved from the data source.

The DataColumn class provides additional properties, such as ExtendedProperties, which is a collection you can use to store miscellaneous information, and ColumnMapping, which allows you to configure the XML representation for each column in a DataTable using a value from the MappingType enumeration. Finally, the Expression property allows you to define a calculated or aggregate column or one that is stored as an expression or calculation. For example, you use a statement such as:

dt.Columns("Total").Expression = "Price * 1.15";

to define an expression for a column that shows the tax-adjusted value ...

Get ADO.NET in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.