DataRowCollection.Add 方法

定義

DataRow 加入至 DataRowCollection

多載

Add(DataRow)

將指定的 DataRow 加入至 DataRowCollection 物件中。

Add(Object[])

使用指定的值建立資料列,並將它加入至 DataRowCollection

Add(DataRow)

來源:
DataRowCollection.cs
來源:
DataRowCollection.cs
來源:
DataRowCollection.cs

將指定的 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)

參數

row
DataRow

要加入的 DataRow

例外狀況

資料列為 null。

資料列不是屬於另一個資料表,就是已經屬於這個資料表。

新增會使條件約束 (Constraint) 失效。

新增會嘗試將 Null 置於 DataColumn 為 False 的 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,會使用父 DataTable代的架構傳回新的 DataRow 物件。 在您建立 DataRow 物件並設定其每個數據行的值之後,請使用 Add 方法將物件新增至集合。

如果使用者在 事件中 RowChanging 產生例外狀況,就會產生例外狀況。 如果發生例外狀況,數據列就不會加入數據表中。

另請參閱

適用於

Add(Object[])

來源:
DataRowCollection.cs
來源:
DataRowCollection.cs
來源:
DataRowCollection.cs

使用指定的值建立資料列,並將它加入至 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[]

用來建立新資料列之值的陣列。

傳回

新的數據列。

例外狀況

陣列大於資料表中的資料行數目。

值不符合其個別的資料行型別。

加入資料列會使條件約束失效。

嘗試將 Null 置於 AllowDBNull 為 False 的資料行中。

範例

下列範例會 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 以取得該數據行的預設值。

如果您在 或 RowChanging 事件期間ColumnChanging產生例外狀況,也可能會發生例外狀況。 如果發生例外狀況,數據列就不會加入數據表中。

另請參閱

適用於