Export (0) Print
Expand All

DataRowCollection.Add Method (Object[])

Creates a row using specified values and adds it to the DataRowCollection.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

public DataRow Add(
	params Object[] values
)

Parameters

values
Type: System.Object[]

The array of values that are used to create the new row.

Return Value

Type: System.Data.DataRow
None.

ExceptionCondition
ArgumentException

The array is larger than the number of columns in the table.

InvalidCastException

A value does not match its respective column type.

ConstraintException

Adding the row invalidates a constraint.

NoNullAllowedException

Trying to put a null in a column where AllowDBNull is false.

If a DataColumn object has its AutoIncrement set to True, null should be passed to get the default value for that column.

Exceptions can also occur if you generate an exception during either a ColumnChanging or RowChanging event. If an exception occurs, the row is not added to the table.

The following example uses the Add method to create and add a new DataRow object to a DataRowCollection.

private void AddRow(DataTable table)
{
    // Create an array with three elements.
    object[] rowVals = new object[3];
    DataRowCollection rowCollection = table.Rows;
    rowVals[0] = "hello";
    rowVals[1] = "world";
    rowVals[2] = "two";

    // Add and return the new row.
    DataRow row = rowCollection.Add(rowVals);
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft