DataRowCollection.Add Metodo

Definizione

Aggiunge un oggetto DataRow a DataRowCollection.

Overload

Add(DataRow)

Aggiunge l'oggetto DataRow specificato all'oggetto DataRowCollection.

Add(Object[])

Crea una riga utilizzando i valori specificati e la aggiunge all'insieme DataRowCollection.

Add(DataRow)

Source:
DataRowCollection.cs
Source:
DataRowCollection.cs
Source:
DataRowCollection.cs

Aggiunge l'oggetto DataRow specificato all'oggetto 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)

Parametri

row
DataRow

Oggetto DataRow da aggiungere.

Eccezioni

La riga è null.

La riga appartiene a un'altra tabella o appartiene già a questa tabella.

L'aggiunta invalida un vincolo.

Con l'aggiunta si tenta di immettere un valore null in un oggetto DataColumn in cui AllowDBNull è impostata su false.

Esempio

Nell'esempio seguente viene utilizzato il Add metodo per aggiungere un nuovo DataRow oggetto a un DataRowCollection oggetto .

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

Commenti

Per creare un nuovo DataRowoggetto , è necessario utilizzare il NewRow metodo della DataTable classe . Quando si usa il NewRow metodo , viene restituito un nuovo DataRow oggetto usando lo schema di padre DataTable. Dopo aver creato l'oggetto DataRow e impostato i valori per ognuna delle relative colonne, utilizzare il Add metodo per aggiungere l'oggetto alla raccolta.

Genera un'eccezione se l'utente genera un'eccezione nell'evento RowChanging . Se si verifica un'eccezione, la riga non viene aggiunta alla tabella.

Vedi anche

Si applica a

Add(Object[])

Source:
DataRowCollection.cs
Source:
DataRowCollection.cs
Source:
DataRowCollection.cs

Crea una riga utilizzando i valori specificati e la aggiunge all'insieme 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

Parametri

values
Object[]

Matrice di valori utilizzati per creare la nuova riga.

Restituisce

Nuova riga.

Eccezioni

La matrice è maggiore del numero di colonne nella tabella.

Un valore non corrisponde al rispettivo tipo di colonna.

L'aggiunta della riga invalida un vincolo.

Tentativo di immissione di un valore null in una colonna in cui la proprietà AllowDBNull è impostata su false.

Esempio

Nell'esempio seguente viene utilizzato il Add metodo per creare e aggiungere un nuovo DataRow oggetto a un oggetto 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

Commenti

Se un DataColumn oggetto è AutoIncrement impostato su True, è necessario passare Null per ottenere il valore predefinito per tale colonna.

Le eccezioni possono verificarsi anche se si genera un'eccezione durante un ColumnChanging evento o RowChanging . Se si verifica un'eccezione, la riga non viene aggiunta alla tabella.

Vedi anche

Si applica a