DataRowCollection.Add 方法

定義

DataRow 加入至 DataRowCollectionAdds a DataRow to the DataRowCollection.

多載

Add(DataRow)

將指定的 DataRow 加入至 DataRowCollection 物件中。Adds the specified DataRow to the DataRowCollection object.

Add(Object[])

使用指定的值建立資料列,並將它加入至 DataRowCollectionCreates a row using specified values and adds it to the DataRowCollection.

Add(DataRow)

將指定的 DataRow 加入至 DataRowCollection 物件中。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)

參數

row
DataRow

要加入的 DataRowThe DataRow to add.

例外狀況

資料列為 null。The row is null.

資料列不是屬於另一個資料表,就是已經屬於這個資料表。The row either belongs to another table or already belongs to this table.

新增會使條件約束 (Constraint) 失效。The addition invalidates a constraint.

新增會嘗試將 Null 置於 DataColumn 為 False 的 AllowDBNull 中。The addition tries to put a null in a DataColumn where AllowDBNull is false.

範例

下列範例會使用 Add 方法,將新的 DataRow 加入至 DataRowCollection 物件。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

備註

若要建立新的 DataRow,您必須使用 DataTable 類別的 NewRow 方法。To create a new DataRow, you must use the NewRow method of the DataTable class. 當您使用 NewRow 方法時,會使用父 DataTable的架構來傳回新的 DataRow 物件。When you use the NewRow method, a new DataRow object is returned using the schema of parent DataTable. 建立 DataRow 物件並設定它的每個資料行的值之後,請使用 Add 方法,將物件加入至集合。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.

如果使用者在 RowChanging 事件中產生例外狀況,就會產生例外狀況。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.

另請參閱

Add(Object[])

使用指定的值建立資料列,並將它加入至 DataRowCollectionCreates a row using specified values and adds it to the DataRowCollection.

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

參數

values
Object[]

用來建立新資料列之值的陣列。The array of values that are used to create the new row.

傳回

None。None.

例外狀況

陣列大於資料表中的資料行數目。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.

嘗試將 Null 置於 AllowDBNull 為 False 的資料行中。Trying to put a null in a column where AllowDBNull is false.

範例

下列範例會使用 Add 方法來建立新的 DataRow 物件,並將其加入至 DataRowCollectionThe 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

備註

如果 DataColumn 物件的 AutoIncrement 設定為 True,則應該傳遞 null 來取得該資料行的預設值。If a DataColumn object has its AutoIncrement set to True, null should be passed to get the default value for that column.

如果您在 ColumnChangingRowChanging 事件期間產生例外狀況,也可能會發生例外狀況。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.

另請參閱

適用於