AddNewRow Method

Overview

The AddNewRow method creates a new IDataRow, initializes it using the table column metadata, sets its state to Added, inserts it into the table, and returns it to the caller.

This method also ensures that the table contains a client-only key column (by convention, SpecialColumnNames.CLIENT_KEY). The new row receives a new Guid value in that column. The client key is used to correlate rows between the client data set and a changeset during merge operations.

Declaration

There are two overloads of the AddNewRow method. The firs one returns IDataRow instance and the second one returns IDataRow instance as a specified interface, supporting live POCO interface projections. The second overload is useful when you have a POCO interface defined for the table rows and want to work with the new row through that interface.

/// <summary>
/// Creates new data row in data table with default values taken from columns metadata
/// </summary>
/// <param name="dataTable">Data table</param>
/// <returns>New data row created in data table</returns>
public static IDataRow AddNewRow(this IDataTable? dataTable)

/// <summary>
/// Creates new data row in data table with default values taken from columns metadata
/// and returns the new row as a live POCO interface projection
/// </summary>
/// <typeparam name="TInterface">POCO interface type</typeparam>
/// <param name="dataTable">Data table</param>
/// <returns>New data row created in data table as a live POCO interface projection</returns>
public static TInterface? AddNewRow(this IDataTable? dataTable) where TInterface : class

Namespace and Assembly

Namespace: PocoDataSet.Extensions
Assembly: PocoDataSet.Extensions.dll

Behavior

Usage Example

The following example returns newly added data row as IDataRow:

// 1. Create an empty data set
IDataSet dataSet = DataSetFactory.CreateDataSet();

// 2. Create an empty Department table
IDataTable departmentDataTable = dataSet.AddNewTable("Department");
departmentDataTable.AddColumn("Id", DataTypeNames.INT32);
departmentDataTable.AddColumn("Name", DataTypeNames.STRING);

// 3. Add a new row to Department table
IDataRow departmentDataRow = departmentDataTable.AddNewRow();

The following example returns newly added data row as an IEmploymentType interface:

/// <summary>
/// Defines employment type functionality
/// </summary>
internal interface IEmploymentType
{
  string Code { get; set; }
  string? Description { get; set; }
  int Id { get; set; }
}

// 1. Create a new data set
IDataSet dataSet = DataSetFactory.CreateDataSet();

// 2. Call AddNewTableFromPocoInterface to add a new table named "EmploymentType" with schema from IEmploymentType interface
IDataTable employmentTypeDataTable = dataSet.AddNewTableFromPocoInterface("EmploymentType", typeof(IEmploymentType));

// 3. Add a new row to EmploymentType table and get it as IEmploymentType
IEmploymentType newEmploymentType = employmentTypeDataTable.AddNewRow();

Notes

 

Table of Content POCO DataSet DataTable Extensions Group DataTable 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.