DataRowCollection.Add 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將 DataRow 加入至 DataRowCollection。
多載
| 名稱 | Description |
|---|---|
| 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)
參數
例外狀況
該列為零。
該列要麼屬於另一張表格,要麼已經屬於該表格。
這個加法使限制失效。
加法嘗試在 中 DataColumn 放入一個空,其中 AllowDBNull 為假。
範例
以下範例使用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[]
用來建立新列的數值陣列。
傳回
新一排。
例外狀況
陣列大小超過表格中的欄位數。
值與其對應的欄位類型不符。
新增該列會使限制失效。
嘗試在 為 false 的欄位 AllowDBNull 中放入 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 以取得該欄位的預設值。
如果你在事件ColumnChangingRowChanging中產生例外,也可能發生例外。 若發生例外,該列不會被加入資料表。