DataRowCollection.Add Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Agrega un DataRow al DataRowCollection.
Sobrecargas
| Nombre | Description |
|---|---|
| Add(DataRow) |
Agrega el objeto especificado DataRow al DataRowCollection objeto . |
| Add(Object[]) |
Crea una fila con valores especificados y lo agrega a .DataRowCollection |
Add(DataRow)
Agrega el objeto especificado DataRow al 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
Excepciones
La fila es null.
La fila pertenece a otra tabla o ya pertenece a esta tabla.
La adición invalida una restricción.
La adición intenta colocar un valor NULL en un donde DataColumnAllowDBNull es false.
Ejemplos
En el ejemplo siguiente se usa el Add método para agregar un nuevo DataRow a un 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
Comentarios
Para crear un nuevo DataRow, debe 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 de 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
Add(Object[])
Crea una fila con valores especificados y lo agrega a .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[]
Matriz de valores que se usan 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 correspondiente.
Agregar la fila invalida una restricción.
Intentando colocar un valor NULL en una columna donde AllowDBNull es false.
Ejemplos
En el ejemplo siguiente se usa el Add método para crear y agregar un nuevo DataRow objeto a .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
Comentarios
Si un DataColumn objeto tiene su AutoIncrement valor establecido en True, se debe pasar null para obtener el valor predeterminado de esa columna.
También se pueden producir excepciones si genera una excepción durante un ColumnChanging evento o RowChanging . Si se produce una excepción, la fila no se agrega a la tabla.