Export (0) Print
Expand All

DataRowCollection.Add Method (DataRow)

Adds the specified DataRow to the DataRowCollection object.

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

public void Add (
	DataRow row
)
public void Add (
	DataRow row
)
public function Add (
	row : DataRow
)
Not applicable.

Parameters

row

The DataRow to add.

Return Value

None.

Exception typeCondition

ArgumentNullException

The row is null.

ArgumentException

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

ConstraintException

The addition invalidates a constraint.

NoNullAllowedException

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

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.

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

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft