DataRowCollection.Add メソッド

定義

DataRowDataRowCollection に追加します。

オーバーロード

Add(DataRow)

指定した DataRowDataRowCollection オブジェクトに追加します。

Add(Object[])

指定した値を使用して行を作成し、この行を DataRowCollection に追加します。

Add(DataRow)

指定した DataRowDataRowCollection オブジェクトに追加します。

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)

パラメーター

row
DataRow

追加する DataRow

例外

行が null です。

この行は他のテーブルに属しているか、既にこのテーブルに属しています。

この行を追加すると、制約が無効になります。

この行を追加すると、DataColumn が false である AllowDBNull に null を格納することになります。

次の例では、 メソッドを Add 使用してオブジェクトに新 DataRow しい を DataRowCollection 追加します。

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

注釈

DataRowしい を作成するには、 クラスの メソッドを NewRow 使用する DataTable 必要があります。 メソッドをNewRow使用すると、親 DataTableのスキーマを使用して新しいDataRowオブジェクトが返されます。 オブジェクトを DataRow 作成し、各列の値を設定したら、 メソッドを Add 使用してオブジェクトをコレクションに追加します。

ユーザーが イベントで例外を生成した場合に例外を生成します RowChanging 。 例外が発生した場合、行はテーブルに追加されません。

こちらもご覧ください

適用対象

Add(Object[])

指定した値を使用して行を作成し、この行を 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

パラメーター

values
Object[]

新しい行の作成に使用する値の配列。

戻り値

新しい行。

例外

配列がテーブルの列数より大きいです。

値が各列の型と一致していません。

この行を追加すると、制約が無効になります。

AllowDBNull が false である列に null を格納しようとしました。

次の例では、 メソッドを Add 使用して 新しい DataRow オブジェクトを作成し、 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

注釈

オブジェクトに DataColumn True が設定されている場合は AutoIncrement 、その列の既定値を取得するために null を渡す必要があります。

例外は、 または RowChanging イベントのColumnChanging間に例外を生成した場合にも発生する可能性があります。 例外が発生した場合、行はテーブルに追加されません。

こちらもご覧ください

適用対象