DataTable.NewRow DataTable.NewRow DataTable.NewRow DataTable.NewRow Method

Definição

Cria um novo DataRow com o mesmo esquema que a tabela.Creates a new DataRow with the same schema as the table.

public:
 System::Data::DataRow ^ NewRow();
public System.Data.DataRow NewRow ();
member this.NewRow : unit -> System.Data.DataRow
Public Function NewRow () As DataRow

Retornos

Um DataRow com o mesmo esquema que o DataTable.A DataRow with the same schema as the DataTable.

Exemplos

O exemplo a seguir cria uma DataTable, adiciona dois DataColumn objetos que determinam o esquema da tabela e cria vários novos DataRow objetos usando o NewRow método.The following example creates a DataTable, adds two DataColumn objects that determine the table's schema, and creates several new DataRow objects using the NewRow method. Aqueles DataRow objetos, em seguida, são adicionados para o DataRowCollection usando o Add método.Those DataRow objects are then added to the DataRowCollection using the Add method.

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;
}
Private Sub MakeDataTableAndDisplay()
    ' Create new DataTable and DataSource objects.
    Dim table As DataTable = New DataTable()

    ' Declare DataColumn and DataRow variables.
    Dim column As DataColumn 
    Dim row As DataRow 
    Dim view As DataView 

    ' 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.    
    Dim i As Integer
    For i = 0 to 9 
       row = table.NewRow()
       row("id") = i
       row("item") = "item " & i
       table.Rows.Add(row)
    Next
    ' Create a DataView using the DataTable.
    view = New DataView(table)

    ' Set a DataGrid control's DataSource to the DataView.
    DataGrid1.DataSource = view
End Sub

Comentários

Você deve usar o NewRow método para criar novos DataRow objetos com o mesmo esquema que o DataTable.You must use the NewRow method to create new DataRow objects with the same schema as the DataTable. Depois de criar uma DataRow, você pode adicioná-lo para o DataRowCollection, por meio de DataTable do objeto Rows propriedade.After creating a DataRow, you can add it to the DataRowCollection, through the DataTable object's Rows property. Quando você usa NewRow para criar novas linhas, as linhas devem ser adicionadas ou excluídas da tabela de dados antes de chamar Clear.When you use NewRow to create new rows, the rows must be added to or deleted from the data table before you call Clear.

Aplica-se a

Veja também