DataColumn Class
The DataColumn
class represents a column in a DataTable
. It defines the schema for the data within that column, including its name, data type, and constraints.
Key Properties of DataColumn
The DataColumn
class exposes several properties that are crucial for defining and managing columns:
ColumnName
: Gets or sets the name of the column. This name must be unique within theDataTable
.DataType
: Gets or sets the data type of the values in the column. This can be any valid .NET data type (e.g.,string
,int
,DateTime
).AllowDBNull
: Gets or sets a value indicating whether the column can containnull
values.Unique
: Gets or sets a value indicating whether each value in the column must be unique.MaxLength
: Gets or sets the maximum length of the string values in the column. Applicable to string-based data types.DefaultValue
: Gets or sets the default value for the column.AutoIncrement
: Gets or sets a value indicating whether the column automatically generates sequential values.Expression
: Gets or sets an expression used to filter or aggregate values in the column.ReadOnly
: Gets or sets a value indicating whether the column is read-only.Table
: Gets theDataTable
that contains this column.
Creating and Adding DataColumns
You can create a DataColumn
object and add it to a DataTable
's Columns
collection. Here's an example:
using System.Data;
// Create a new DataTable
DataTable myTable = new DataTable("Products");
// Create a DataColumn for Product ID (integer, primary key)
DataColumn idColumn = new DataColumn();
idColumn.ColumnName = "ProductID";
idColumn.DataType = typeof(int);
idColumn.AutoIncrement = true;
idColumn.AutoIncrementSeed = 1;
idColumn.AutoIncrementStep = 1;
idColumn.Unique = true;
myTable.Columns.Add(idColumn);
// Create a DataColumn for Product Name (string, not null)
DataColumn nameColumn = new DataColumn("ProductName", typeof(string));
nameColumn.AllowDBNull = false;
nameColumn.MaxLength = 100;
myTable.Columns.Add(nameColumn);
// Create a DataColumn for Price (decimal, non-nullable)
DataColumn priceColumn = new DataColumn("Price", typeof(decimal));
priceColumn.AllowDBNull = false;
priceColumn.DefaultValue = 0.00;
myTable.Columns.Add(priceColumn);
// Set Primary Key
DataColumn[] primaryKeyColumns = new DataColumn[1];
primaryKeyColumns[0] = myTable.Columns["ProductID"];
myTable.PrimaryKey = primaryKeyColumns;
Console.WriteLine($"DataTable '{myTable.TableName}' created with columns:");
foreach (DataColumn col in myTable.Columns)
{
Console.WriteLine($"- {col.ColumnName} (Type: {col.DataType.Name}, AllowDBNull: {col.AllowDBNull}, Unique: {col.Unique})");
}
Working with DataColumn Values
Once columns are defined, you can populate and manipulate data within them using DataRow
objects.
// Add some rows to the DataTable
DataRow row1 = myTable.NewRow();
row1["ProductName"] = "Laptop";
row1["Price"] = 1200.50m;
myTable.Rows.Add(row1);
DataRow row2 = myTable.NewRow();
row2["ProductName"] = "Keyboard";
row2["Price"] = 75.00m;
myTable.Rows.Add(row2);
Console.WriteLine("\nData in the DataTable:");
foreach (DataRow row in myTable.Rows)
{
Console.WriteLine($"ID: {row["ProductID"]}, Name: {row["ProductName"]}, Price: {row["Price"]}");
}
// Update a value
myTable.Rows[0]["Price"] = 1150.75m;
Console.WriteLine("\nUpdated Price for Laptop:");
Console.WriteLine($"ID: {myTable.Rows[0]["ProductID"]}, Name: {myTable.Rows[0]["ProductName"]}, Price: {myTable.Rows[0]["Price"]}");
Important: When setting the
DataType
, ensure it matches the data you intend to store. Incorrect data types can lead to runtime errors. Use appropriate .NET types for optimal performance and type safety.
Common Scenarios
- Defining schema for data retrieved from databases or other sources.
- Creating in-memory data structures for processing.
- Enforcing data integrity rules like uniqueness and non-nullability.
- Performing calculations or aggregations using column expressions.
The DataColumn
class is fundamental to the ADO.NET data model, providing the structure and rules for the data contained within a DataTable
.