DataRowCollection.Add Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Adiciona um DataRow ao DataRowCollection.
Sobrecargas
| Name | Description |
|---|---|
| Add(DataRow) |
Adiciona o especificado DataRow ao DataRowCollection objeto. |
| Add(Object[]) |
Cria uma linha usando valores especificados e adiciona-a ao DataRowCollection. |
Add(DataRow)
Adiciona o especificado DataRow ao DataRowCollection objeto.
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)
Parâmetros
Exceções
A linha é nula.
A linha pertence a outra tabela ou já pertence a esta tabela.
A adição invalida uma restrição.
A adição tenta colocar um nulo em um DataColumn onde AllowDBNull é falso.
Exemplos
O exemplo seguinte usa o Add método para adicionar um novo DataRow a um DataRowCollection objeto.
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
Observações
Para criar um novo DataRow, deve usar o NewRow método da DataTable classe. Quando usa o NewRow método, um novo DataRow objeto é devolvido usando o esquema do pai DataTable. Depois de criares o DataRow objeto e definires os valores de cada uma das suas colunas, usa o Add método para adicionar o objeto à coleção.
Gera uma exceção se o utilizador gerar uma exceção no RowChanging evento. Se ocorrer uma exceção, a linha não é adicionada à tabela.
Ver também
Aplica-se a
Add(Object[])
Cria uma linha usando valores especificados e adiciona-a ao DataRowCollection.
public:
virtual System::Data::DataRow ^ Add(cli::array <System::Object ^> ^ values);
public:
System::Data::DataRow ^ Add(... cli::array <System::Object ^> ^ values);
public virtual System.Data.DataRow Add(object[] values);
public System.Data.DataRow Add(params object[] values);
abstract member Add : obj[] -> System.Data.DataRow
override this.Add : obj[] -> System.Data.DataRow
member this.Add : obj[] -> System.Data.DataRow
Public Overridable Function Add (values As Object()) As DataRow
Public Function Add (ParamArray values As Object()) As DataRow
Parâmetros
- values
- Object[]
O array de valores que são usados para criar a nova linha.
Devoluções
A nova fila.
Exceções
O array é maior do que o número de colunas na tabela.
Um valor não corresponde ao respetivo tipo de coluna.
Adicionar a linha invalida uma restrição.
Tentar colocar um nulo numa coluna onde AllowDBNull é falso.
Exemplos
O exemplo seguinte utiliza o Add método para criar e adicionar um novo DataRow objeto a um 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
Observações
Se um DataColumn objeto tiver AutoIncrement o seu valor definido como True, o null deve ser passado para obter o valor padrão dessa coluna.
Exceções também podem ocorrer se gerar uma exceção durante um ColumnChanging evento de ou.RowChanging Se ocorrer uma exceção, a linha não é adicionada à tabela.