Rellenar un dataTable sin pasar por un DataReader

2300 vistas

Veamos un ejemplo donde creamos y rellenamos un DataTable con code-behind



csharp
  1. DataRow tuple;
  2. DataTable table = new DataTable();
  3. DataColumn col1 = new DataColumn("Col1", Type.GetType(integer));
  4. DataColumn col2 = new DataColumn("Col", Type.GetType(string));
  5. // añadimos las columnas al dataTable
  6. table.Columns.Add(col1) ;
  7. table.Columns.Add(col2) ;
  8. // creamos una fila
  9. tuple = table.NewRow();
  10. // la rellenamos
  11. tuple.Item[0] = 1;
  12. tuple.Item[1] = "Primer registro del dataTable";
  13. // lo añadimos a la tabla
  14. table.Rows.Add(tuple);
  15. // creamos una fila
  16. tuple = table.NewRow();
  17. // la rellenamos
  18. tuple.Item[0] = 2;
  19. tuple.Item[1] = "Segundo registro del dataTable";
  20. // lo añadimos a la tabla
  21. table.Rows.Add(tuple);