Leer en inglés

Compartir vía


DataTable.NewRow Método

Definición

Crea un nuevo DataRow con el mismo esquema que la tabla.

C#
public System.Data.DataRow NewRow ();

Devoluciones

DataRow con el mismo esquema que DataTable.

Ejemplos

En el ejemplo siguiente se crea un DataTableobjeto , se agregan dos DataColumn objetos que determinan el esquema de la tabla y se crean varios objetos nuevos DataRow mediante el NewRow método . A continuación, esos DataRow objetos se agregan al DataRowCollection mediante el Add método .

C#
private void MakeDataTableAndDisplay()
{
    // Create new DataTable and DataSource objects.
    DataTable table = new DataTable();

    // Declare DataColumn and DataRow variables.
    DataColumn column;
    DataRow row;
    DataView view;

    // Create new DataColumn, set DataType, ColumnName and add to DataTable.
    column = new DataColumn();
    column.DataType = System.Type.GetType("System.Int32");
    column.ColumnName = "id";
    table.Columns.Add(column);

    // Create second column.
    column = new DataColumn();
    column.DataType = Type.GetType("System.String");
    column.ColumnName = "item";
    table.Columns.Add(column);

    // Create new DataRow objects and add to DataTable.
    for(int i = 0; i < 10; i++)
    {
        row = table.NewRow();
        row["id"] = i;
        row["item"] = "item " + i.ToString();
        table.Rows.Add(row);
    }

    // Create a DataView using the DataTable.
    view = new DataView(table);

    // Set a DataGrid control's DataSource to the DataView.
    dataGrid1.DataSource = view;
}

Comentarios

Debe usar el NewRow método para crear nuevos DataRow objetos con el mismo esquema que .DataTable Después de crear un DataRow, puede agregarlo a , DataRowCollectiona través de la DataTable propiedad del Rows objeto . Cuando se usa NewRow para crear nuevas filas, las filas se deben agregar o eliminar de la tabla de datos antes de llamar a Clear.

Se aplica a

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

Consulte también