AddColumn Method

Overview

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).

Declaration

/// <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 and Assembly

Namespace: PocoDataSet.ObservableExtensions
Assembly: PocoDataSet.ObservableExtensions.dll

Behavior

Usage Examples

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);

Notes

 

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.