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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft