DataRowCollection.Add Método

Definição

Adiciona um DataRow ao DataRowCollection.

Sobrecargas

Add(DataRow)

Adiciona o DataRow especificado ao objeto DataRowCollection.

Add(Object[])

Cria uma linha usando os valores especificados e a adiciona ao DataRowCollection.

Add(DataRow)

Adiciona o DataRow especificado ao objeto 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)

Parâmetros

row
DataRow

O DataRow a ser adicionado.

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 valor nulo em um DataColumn em que AllowDBNull é falso.

Exemplos

O exemplo a seguir 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

Comentários

Para criar um novo DataRow, você deve usar o NewRow método da DataTable classe . Quando você usa o NewRow método , um novo DataRow objeto é retornado usando o esquema do pai DataTable. Depois de criar o DataRow objeto e definir os valores de cada uma de suas colunas, use o Add método para adicionar o objeto à coleção.

Gerará uma exceção se o usuário gerar uma exceção no RowChanging evento. Se ocorrer uma exceção, a linha não será adicionada à tabela.

Confira também

Aplica-se a

Add(Object[])

Cria uma linha usando os valores especificados e a adiciona ao 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

Parâmetros

values
Object[]

Uma matriz de valores usados para criar a nova linha.

Retornos

A nova linha.

Exceções

A matriz é maior que o número de colunas na tabela.

Um valor não corresponde a seu respectivo tipo de coluna.

Adicionar a linha invalida uma restrição.

Tentativa de colocar um valor nulo em uma coluna em que AllowDBNull é falso.

Exemplos

O exemplo a seguir usa 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

Comentários

Se um DataColumn objeto tiver seu AutoIncrement conjunto como True, nulo deverá ser passado para obter o valor padrão dessa coluna.

Exceções também podem ocorrer se você gerar uma exceção durante um ColumnChanging evento ou RowChanging . Se ocorrer uma exceção, a linha não será adicionada à tabela.

Confira também

Aplica-se a