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)
Parameters
- values
- Type: System.Object[]
The array of values that are used to create the new row.
| Exception | Condition |
|---|---|
| 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 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.