AddColumn Method
The AddColumn method adds a column definition (IColumnMetadata) to an
observable data table. In addition to adding metadata, it also ensures that all existing rows
in the table receive the new column (with an appropriate default value).
The method supports optional flags for nullability, primary key, and foreign key.
When these flags are not provided, the method applies simple conventions based on the column name
(for example, treating Id as a primary key, and treating columns that end with Id
as foreign keys).
/// <summary>
/// Adds a column to observable table (delegates to inner table)
/// </summary>
/// <param name="observableDataTable">Observable data table</param>
/// <param name="columnName">Column name</param>
/// <param name="dataType">Data type</param>
/// <param name="isNullable">Flag indicating whether column is nullable</param>
/// <param name="isPrimaryKey">Flag indicating whether column is primary key</param>
/// <param name="isForeignKey">Flag indicating whether column is foreign key</param>
public static void AddColumn(this IObservableDataTable? dataTable, string columnName, string dataType, bool? isNullable = null, bool? isPrimaryKey = null, bool? isForeignKey = null)
Namespace: PocoDataSet.ObservableExtensions
Assembly: PocoDataSet.ObservableExtensions.dll
dataTable is null, returns immediately.columnName and dataType.dataTable.Columns.
If a column with the same name exists, throws KeyDuplicationException.
ColumnMetadata and assigns:
ColumnNameDataTypeisPrimaryKey is provided, uses it.IsPrimaryKey = true only when columnName is exactly "Id".isNullable is provided, uses it.IsNullable = false for primary key columns; for other columns, defaults to true.isForeignKey is provided, uses it.IsForeignKey = true when
columnName ends with "Id" (case-sensitive, ordinal comparison) and is not exactly "Id".
ReferencedColumnName = "Id"ReferencedTableName as columnName without the trailing "Id" suffixDepartmentId will reference table Department, column Id.
dataTable.Columns and calls
EnsureExistingRowsHaveColumn(dataTable, columnName) to extend all existing rows with the new column.
Add several columns using name-based inference:
// 1. Create observable data set
IObservableDataSet observableDataSet = new ObservableDataSet();
// 2. Create Employee observable table
IObservableDataTable employeeObservableDataTable = observableDataSet.AddNewTable("Employee");
// 3) Add columns
// - "Id" becomes a primary key, non-nullable
IColumnMetadata idColumnMetadata = employeeObservableDataTable.AddColumn("Id", DataTypeNames.INT32);
// - "FirstName" is nullable by default (unless overridden)
IColumnMetadata firstNameColumnMetadata = employeeObservableDataTable.AddColumn("FirstName", DataTypeNames.STRING);
// - "EmployeeTypeId" becomes a foreign key by convention
IColumnMetadata employeeTypeIdColumnMetadata = employeeObservableDataTable.AddColumn("EmployeeTypeId", DataTypeNames.INT32);
// Adds a non-nullable foreign key column, "SecurityLevelId"
IColumnMetadata securityLevelIdColumnMetadata = employeeObservableDataTable.AddColumn("SecurityLevelId", DataTypeNames.INT32, false, false, true);
IColumnMetadata yourself or using schema-loading APIs.
EnsureExistingRowsHaveColumn is invoked, adding a column to a table with many existing rows may have a performance impact.
(Each existing row is extended with the new column.)
Table of Content POCO DataSet Observable Data Table Members
Business Process Programming in .Net
© 2004–2026 Laskarzhevsky Software Inc.
Unless otherwise noted, the content of this website is licensed under the
Creative Commons Attribution 4.0 International License (CC BY 4.0).
Code examples are provided under the MIT License.
You are free to share and adapt the material provided that appropriate
credit is given and any modifications are clearly indicated.
The information provided on this website is for educational purposes only.
The author and publisher make no warranties regarding the completeness
or suitability of the information and are not responsible for any damages
resulting from its use.