Leer en inglés

Compartir vía


DataRowCollection.Add Método

Definición

Agrega un objeto DataRow a DataRowCollection.

Sobrecargas

Add(DataRow)

Agrega el DataRow especificado al objeto DataRowCollection.

Add(Object[])

Crea una fila mediante los valores especificados y la agrega a DataRowCollection.

Add(DataRow)

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

Agrega el DataRow especificado al objeto DataRowCollection.

C#
public void Add (System.Data.DataRow row);

Parámetros

row
DataRow

DataRow que se va a agregar.

Excepciones

La fila es nula.

La fila pertenece a otra tabla o ya pertenece a ésta.

La adición invalida una restricción.

La adición intenta colocar un valor nulo en DataColumn donde AllowDBNull sea falso.

Ejemplos

En el ejemplo siguiente se usa el Add método para agregar un nuevo DataRow objeto a un DataRowCollection objeto .

C#
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);
}

Comentarios

Para crear un objeto , DataRowdebe usar el NewRow método de la DataTable clase . Cuando se usa el NewRow método , se devuelve un nuevo DataRow objeto mediante el esquema del elemento primario DataTable. Después de crear el DataRow objeto y establecer los valores de cada una de sus columnas, use el Add método para agregar el objeto a la colección.

Genera una excepción si el usuario genera una excepción en el RowChanging evento . Si se produce una excepción, la fila no se agrega a la tabla.

Consulte también

Se aplica a

.NET 9 otras versiones
Producto Versiones
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(Object[])

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

Crea una fila mediante los valores especificados y la agrega a DataRowCollection.

C#
public System.Data.DataRow Add (params object?[] values);
C#
public System.Data.DataRow Add (params object[] values);
C#
public virtual System.Data.DataRow Add (object[] values);

Parámetros

values
Object[]

Matriz de valores que se utiliza para crear la nueva fila.

Devoluciones

Nueva fila.

Excepciones

La matriz es mayor que el número de columnas de la tabla.

Un valor no coincide con su tipo de columna respectivo.

La adición de la fila invalida una restricción.

Se ha intentado colocar un valor null en una columna en la que AllowDBNull es false.

Ejemplos

En el ejemplo siguiente se usa el Add método para crear y agregar un nuevo DataRow objeto a .DataRowCollection

C#
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);
}

Comentarios

Si un DataColumn objeto tiene establecido AutoIncrement en True, se debe pasar null para obtener el valor predeterminado de esa columna.

También se pueden producir excepciones si se genera una excepción durante un ColumnChanging evento o RowChanging . Si se produce una excepción, la fila no se agrega a la tabla.

Consulte también

Se aplica a

.NET 9 otras versiones
Producto Versiones
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1