DataColumn Class

Represents a column in a DataTable. The DataColumn class defines the schema for a column, including its data type, constraints, and default value.

Namespace: System.Data

Assembly: System.Data.dll

Syntax

public class DataColumn : MarshalByRefObject, ICloneable

Remarks

A DataColumn is an integral part of a DataTable. It specifies the name, data type, and other properties of the data that can be contained within that column for each DataRow in the table. You can define constraints on a column, such as uniqueness or foreign key relationships, by using the Unique property or by adding Constraint objects to the DataTable.Constraints collection.

When creating a DataColumn, you must specify its name and data type. For example:

DataTable myTable = new DataTable();
DataColumn idColumn = new DataColumn("ID", typeof(int));
DataColumn nameColumn = new DataColumn("Name", typeof(string));
myTable.Columns.Add(idColumn);
myTable.Columns.Add(nameColumn);

Properties

Name Description
AllowDBNull Gets or sets a value indicating whether the column allows null values.
AutoIncrement Gets or sets a value indicating whether the column automatically increments.
AutoIncrementSeed Gets or sets the initial value for an auto-incrementing column.
AutoIncrementStep Gets or sets the increment step for an auto-incrementing column.
Caption Gets or sets the caption for the column.
ColumnMapping Gets or sets how the column is mapped for XmlRead and XmlWrite.
ColumnName Gets or sets the name of the column.
DataType Gets or sets the data type of the column.
DefaultValue Gets or sets the default value for the column.
Expression Gets or sets the expression used to filter or aggregate the column's values.
ExtendedProperties Gets a collection of extended properties for the column.
MaxLength Gets or sets the maximum length of the column's values.
Namespace Gets or sets the XML namespace for the column.
Ordinal Gets the zero-based index of the column within the DataColumnCollection.
ReadOnly Gets or sets a value indicating whether the column is read-only.
Table Gets the DataTable to which this column belongs.
Unique Gets or sets a value indicating whether values in the column must be unique.

Methods

Name Description
Clone() Creates a new DataColumn with the same properties as this instance.
Equals(object value) Determines whether the specified object is equal to the current object.
GetHashCode() Serves as the default hash function.
GetType() Gets the type of the current instance.
ToString() Returns a string that represents the current object.
Note: The DataColumn class does not expose constructors directly for typical usage. You usually create DataColumn objects by adding them to a DataTable.Columns collection.

See Also