DataRowCollection.Add メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
DataRowにDataRowCollectionを追加します。
オーバーロード
| 名前 | 説明 |
|---|---|
| Add(DataRow) |
指定した DataRow を DataRowCollection オブジェクトに追加します。 |
| Add(Object[]) |
指定した値を使用して行を作成し、 DataRowCollectionに追加します。 |
Add(DataRow)
指定した DataRow を DataRowCollection オブジェクトに追加します。
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)
パラメーター
例外
行が 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 メソッドを使用すると、親DataRowのスキーマを使用して新しいDataTable オブジェクトが返されます。 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 オブジェクトのAutoIncrementが True に設定されている場合、その列の既定値を取得するには null を渡す必要があります。
例外は、 ColumnChanging または RowChanging イベントの間に例外を生成した場合にも発生する可能性があります。 例外が発生した場合、行はテーブルに追加されません。