MSDN Documentation

ADO.NET Concepts

DataColumn Class

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

Purpose and Functionality

Each DataColumn object corresponds to a column in a DataTable. It holds metadata about the column, such as:

Creating and Configuring DataColumns

You can create a DataColumn and add it to a DataTable in several ways. Here's an example of programmatically creating a DataTable with a DataColumn:


using System;
using System.Data;

// Create a new DataTable
DataTable customersTable = new DataTable("Customers");

// Create a DataColumn for CustomerID
DataColumn customerIdColumn = new DataColumn();
customerIdColumn.DataType = typeof(int);
customerIdColumn.ColumnName = "CustomerID";
customerIdColumn.AutoIncrement = true; // Auto-incrementing ID
customerIdColumn.AutoIncrementSeed = 1;
customerIdColumn.AutoIncrementStep = 1;
customerIdColumn.AllowDBNull = false; // Cannot be null
customersTable.Columns.Add(customerIdColumn);

// Create a DataColumn for CustomerName
DataColumn customerNameColumn = new DataColumn();
customerNameColumn.DataType = typeof(string);
customerNameColumn.ColumnName = "CustomerName";
customerNameColumn.MaxLength = 50; // Max 50 characters
customerNameColumn.AllowDBNull = false;
customersTable.Columns.Add(customerNameColumn);

// Create a DataColumn for Email
DataColumn emailColumn = new DataColumn();
emailColumn.DataType = typeof(string);
emailColumn.ColumnName = "Email";
emailColumn.AllowDBNull = true; // Can be null
customersTable.Columns.Add(emailColumn);

// Add a primary key constraint
customersTable.PrimaryKey = new DataColumn[] { customerIdColumn };

// Now you can add rows to customersTable
            

Common Properties

Property Description
AllowDBNull Gets or sets a value indicating whether the column supports null values.
AutoIncrement Gets or sets a value indicating whether the column automatically increments its value when a new row is added.
AutoIncrementSeed Gets or sets the starting value for an auto-incrementing column.
AutoIncrementStep Gets or sets the increment value for an auto-incrementing column.
Caption Gets or sets the display string for the column.
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 rows or compute column values.
ExtendedProperties Gets a collection of custom user-defined properties and their associated values.
IsReadOnly Gets or sets a value indicating whether the column is read-only.
MaxLength Gets or sets the maximum length of the column's values. Only applicable to string data types.
Namespace Gets or sets the namespace for the column.
Ordinal Gets the zero-based index of the column within the DataTable.
Table Gets the DataTable to which this column belongs.
Unique Gets or sets a value indicating whether values in the column must be unique.

Related Topics