DataRowCollection.Add Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Aggiunge un DataRow al DataRowCollection.
Overload
| Nome | Descrizione |
|---|---|
| Add(DataRow) |
Aggiunge l'oggetto specificato DataRow all'oggetto DataRowCollection . |
| Add(Object[]) |
Crea una riga usando i valori specificati e la aggiunge all'oggetto DataRowCollection. |
Add(DataRow)
- Origine:
- DataRowCollection.cs
- Origine:
- DataRowCollection.cs
- Origine:
- DataRowCollection.cs
- Origine:
- DataRowCollection.cs
- Origine:
- DataRowCollection.cs
Aggiunge l'oggetto specificato DataRow 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
Eccezioni
La riga è null.
La riga appartiene a un'altra tabella o appartiene già a questa tabella.
L'aggiunta invalida un vincolo.
L'aggiunta tenta di inserire un valore Null in un dove DataColumnAllowDBNull è 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 usare il NewRow metodo della DataTable classe . Quando si usa il NewRow metodo , viene restituito un nuovo DataRow oggetto usando lo schema dell'elemento 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 all'insieme.
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[])
- Origine:
- DataRowCollection.cs
- Origine:
- DataRowCollection.cs
- Origine:
- DataRowCollection.cs
- Origine:
- DataRowCollection.cs
- Origine:
- DataRowCollection.cs
Crea una riga usando i valori specificati e la aggiunge all'oggetto 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.
Valori restituiti
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 inserire un valore Null in una colonna in cui AllowDBNull è 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.