DataRowCollection.Add Method

Definition

Adds a DataRow to the DataRowCollection.

Overloads

Add(DataRow)

Adds the specified DataRow to the DataRowCollection object.

Add(Object[])

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

Add(DataRow)

Source:
DataRowCollection.cs
Source:
DataRowCollection.cs
Source:
DataRowCollection.cs

Adds the specified DataRow to the DataRowCollection object.

C#
public void Add(System.Data.DataRow row);

Parameters

row
DataRow

The DataRow to add.

Exceptions

The row is null.

The row either belongs to another table or already belongs to this table.

The addition invalidates a constraint.

The addition tries to put a null in a DataColumn where AllowDBNull is false.

Examples

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

C#
private void ShowRows(DataTable table)
{
    // Print the number of rows in the collection.
    Console.WriteLine(table.Rows.Count);
    // Print the value of columns 1 in each row
    foreach(DataRow row in table.Rows)
    {
        Console.WriteLine(row[1]);
    }
}

private void AddRow(DataTable table)
{
    DataRowCollection rowCollection = table.Rows;
    // Instantiate a new row using the NewRow method.

    DataRow newRow = table.NewRow();
    // Insert code to fill the row with values.

    // Add the row to the DataRowCollection.
    table.Rows.Add(newRow);
}

Remarks

To create a new DataRow, you must use the NewRow method of the DataTable class. When you use the NewRow method, a new DataRow object is returned using the schema of parent DataTable. After you create the DataRow object and set the values for each of its columns, use the Add method to add the object to the collection.

Generates an exception if the user generates an exception in the RowChanging event. If an exception occurs, the row is not added to the table.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(Object[])

Source:
DataRowCollection.cs
Source:
DataRowCollection.cs
Source:
DataRowCollection.cs

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

C#
public System.Data.DataRow Add(params object?[] values);
C#
public System.Data.DataRow Add(params object[] values);
C#
public virtual System.Data.DataRow Add(object[] values);

Parameters

values
Object[]

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

Returns

The new row.

Exceptions

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

A value does not match its respective column type.

Adding the row invalidates a constraint.

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

Examples

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

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

Remarks

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.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1