Redigeeri

Jagamisviis:


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.

public:
 void Add(System::Data::DataRow ^ row);
public void Add (System.Data.DataRow row);
member this.Add : System.Data.DataRow -> unit
Public Sub Add (row As DataRow)

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.

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

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

Add(Object[])

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

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

public:
 System::Data::DataRow ^ Add(... cli::array <System::Object ^> ^ values);
public:
 virtual System::Data::DataRow ^ Add(cli::array <System::Object ^> ^ values);
public System.Data.DataRow Add (params object?[] values);
public System.Data.DataRow Add (params object[] values);
public virtual System.Data.DataRow Add (object[] values);
member this.Add : obj[] -> System.Data.DataRow
abstract member Add : obj[] -> System.Data.DataRow
override this.Add : obj[] -> System.Data.DataRow
Public Function Add (ParamArray values As Object()) As DataRow
Public Overridable Function Add (values As Object()) As DataRow

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.

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);
}
Private Sub AddRow(ByVal table As DataTable)
    ' Create an array with three elements.
    Dim rowVals(2) As Object
    Dim rowCollection As DataRowCollection = table.Rows
    rowVals(0) = "hello"
    rowVals(1) = "world"
    rowVals(2) = "two"

    ' Add and return the new row.
    Dim row As DataRow = rowCollection.Add(rowVals) 
End Sub

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