MSDN Documentation

Microsoft Developer Network

DataColumn Class (ADO.NET)

The DataColumn class represents a column within a DataTable. It defines the properties of the column, such as its name, data type, and constraints.

A DataTable is composed of one or more DataColumn objects, each representing a field or attribute of the data. These columns are fundamental to structuring relational data in memory.

Key Properties

Creating and Adding DataColumns

You can create DataColumn objects programmatically and add them to a DataTable. Here's a common way to do this:


using System;
using System.Data;

public class Example
{
    public static void Main(string[] args)
    {
        // Create a new DataTable
        DataTable dataTable = new DataTable("Customers");

        // Create DataColumns
        DataColumn idColumn = new DataColumn();
        idColumn.DataType = typeof(int);
        idColumn.ColumnName = "CustomerID";
        idColumn.ReadOnly = true; // ID is usually read-only
        idColumn.AutoIncrement = true; // Auto-generate IDs

        DataColumn nameColumn = new DataColumn();
        nameColumn.DataType = typeof(string);
        nameColumn.ColumnName = "CustomerName";
        nameColumn.AllowDBNull = false; // Name is required
        nameColumn.MaxLength = 50;

        DataColumn dateColumn = new DataColumn();
        dateColumn.DataType = typeof(DateTime);
        dateColumn.ColumnName = "RegistrationDate";
        dateColumn.DefaultValue = DateTime.Today;

        // Add columns to the DataTable
        dataTable.Columns.Add(idColumn);
        dataTable.Columns.Add(nameColumn);
        dataTable.Columns.Add(dateColumn);

        // You can also create and add in a single step:
        dataTable.Columns.Add("Email", typeof(string), "CustomerName + '@example.com'");
        // The last parameter is an expression for computed columns

        Console.WriteLine($"DataTable '{dataTable.TableName}' created with {dataTable.Columns.Count} columns.");
    }
}
            

Example Usage

Accessing Column Properties

You can access the properties of a DataColumn after it has been added to a DataTable:


// Assuming 'dataTable' is already populated with columns

DataColumn customerNameCol = dataTable.Columns["CustomerName"];
if (customerNameCol != null)
{
    Console.WriteLine($"Column Name: {customerNameCol.ColumnName}");
    Console.WriteLine($"Data Type: {customerNameCol.DataType.Name}");
    Console.WriteLine($"Allows Nulls: {customerNameCol.AllowDBNull}");
}
                

Constraints and Relations

DataColumn objects are also key to defining constraints (like UniqueConstraint and ForeignKeyConstraint) and relationships between DataTables, which are essential for maintaining data integrity in your application.

For more advanced scenarios, consider exploring concepts like DataView for filtering and sorting, and DataRelation for defining relationships between tables.

Computed Columns

DataColumn supports computed columns, where the value of a column is determined by an expression evaluated over other columns in the same row. This is useful for deriving data on the fly.


// Example: Create a computed column for full name
dataTable.Columns.Add("FullName", typeof(string), "FirstName + ' ' + LastName");