Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

DataRowCollection.Add Method (DataRow)

Adds the specified DataRow to the DataRowCollection object.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)
'Declaration
Public Sub Add ( _
	row As DataRow _
)

Parameters

row
Type: System.Data.DataRow

The DataRow to add.

ExceptionCondition
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 Sub ShowRows(Byval table As DataTable)
    ' Print the number of rows in the collection.
    Console.WriteLine(table.Rows.Count)

    Dim row  As DataRow
    ' Print the value of columns 1 in each row 
    For Each row In table.Rows
        Console.WriteLine(row(1))
    Next 
End Sub 

Private Sub AddRow(ByVal table As DataTable)
    ' Instantiate a new row using the NewRow method. 
    Dim newRow As DataRow = table.NewRow()
    ' Insert code to fill the row with values. 

    ' Add the row to the DataRowCollection.
    table.Rows.Add(newRow)
End Sub

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, 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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.