DataColumn Class

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

Purpose and Usage

A DataColumn is a fundamental part of the DataTable object, defining the structure and characteristics of the data stored within that table. Each DataColumn represents a field or attribute of the data. You use DataColumn objects to:

  • Define the name and data type of a column.
  • Specify whether a column can contain null values.
  • Set a default value for a column.
  • Apply constraints such as uniqueness or foreign keys.
  • Create computed columns.
  • Define the order of columns in a DataTable.

Key Properties

The DataColumn class offers a rich set of properties to configure its behavior and characteristics:

  • ColumnName: Gets or sets the name of the column. This name must be unique within the DataTable.
  • DataType: Gets or sets the data type of the column. This can be any valid .NET data type, such as string, int, DateTime, etc.
  • AllowDBNull: Gets or sets a Boolean value indicating whether the column can contain null values. Defaults to true.
  • DefaultValue: Gets or sets the default value for the column. This value is used when a new row is added without a value explicitly provided for this column.
  • MaxLength: Gets or sets the maximum length of the data allowed in the column. This is particularly useful for string data types.
  • Unique: Gets or sets a Boolean value indicating whether values in this column must be unique.
  • AutoIncrement: Gets or sets a Boolean value indicating whether the column is an auto-incrementing column.
  • AutoIncrementSeed: Gets or sets the starting value for an auto-incrementing column.
  • AutoIncrementStep: Gets or sets the increment value for an auto-incrementing column.
  • Expression: Gets or sets an expression used to filter rows or calculate a column's value (for computed columns).
  • Table: Gets the DataTable that contains this column.

Creating and Adding DataColumns

You can create a DataColumn and add it to a DataTable in several ways:

Method 1: Using the DataColumn constructor


// Create a DataTable
DataTable myTable = new DataTable("Products");

// Create DataColumns
DataColumn idColumn = new DataColumn("ProductID", typeof(int));
idColumn.AutoIncrement = true;
idColumn.AutoIncrementSeed = 1;
idColumn.AutoIncrementStep = 1;

DataColumn nameColumn = new DataColumn("ProductName", typeof(string));
nameColumn.AllowDBNull = false; // Product name is required
nameColumn.MaxLength = 50;

DataColumn priceColumn = new DataColumn("Price", typeof(decimal));
priceColumn.DefaultValue = 0.00m;

// Add columns to the DataTable
myTable.Columns.Add(idColumn);
myTable.Columns.Add(nameColumn);
myTable.Columns.Add(priceColumn);

// Now you can add rows to myTable
                

Method 2: Using the DataTable.Columns.Add() overload


DataTable myTable = new DataTable("Customers");

// Add columns directly using the Add method
myTable.Columns.Add("CustomerID", typeof(int));
myTable.Columns.Add("FirstName", typeof(string));
myTable.Columns.Add("LastName", typeof(string));
myTable.Columns.Add("RegistrationDate", typeof(DateTime));

// Configure properties after adding
myTable.Columns["CustomerID"].AutoIncrement = true;
myTable.Columns["CustomerID"].AutoIncrementSeed = 1000;
myTable.Columns["FirstName"].AllowDBNull = false;
                

Computed Columns

A computed column's value is derived from an expression that can involve other columns in the same DataTable. This is powerful for creating calculated fields on the fly.


DataTable orderDetailsTable = new DataTable("OrderDetails");
orderDetailsTable.Columns.Add("Quantity", typeof(int));
orderDetailsTable.Columns.Add("UnitPrice", typeof(decimal));

// Add a computed column for the line total
DataColumn lineTotalColumn = new DataColumn("LineTotal", typeof(decimal), "Quantity * UnitPrice");
orderDetailsTable.Columns.Add(lineTotalColumn);

// Example of adding data and accessing the computed column
orderDetailsTable.Rows.Add(5, 10.50m);
orderDetailsTable.Rows.Add(2, 25.00m);

foreach (DataRow row in orderDetailsTable.Rows)
{
    Console.WriteLine($"Line Total: {row["LineTotal"]}"); // Output: Line Total: 52.50, Line Total: 50.00
}
                

Tip: When creating computed columns, ensure that the expression's data type is compatible with the column's declared DataType. If no DataType is explicitly set for a computed column, ADO.NET will attempt to infer it from the expression.

See Also