2009-04-20 1506 views

Respuesta

36
dt = DataView.ToTable() 

O

dt = DataView.Table.Copy(),

O

dt = DataView.Table.Clone();

+1

Gracias, google me estaba fallando bastante mal. Afortunadamente, esta página tendrá un alto puntaje. – Ravedave

+1

Gracias Jose ... Funcionó ... –

+8

Nota: 'DataView.ToTable()' solo copiará los valores de DataView. 'DataView.Table.Copy()' copiará la DataTable de origen, no los datos filtrados de DataView. 'DataView.Table.Clone()' solo copiará la estructura del DataTable de origen. – Homer

3

La respuesta no funciona para mi situación porque tengo columnas con expresiones. DataView.ToTable() solo copiará los valores, no las expresiones.

Primero probé esto:

//clone the source table 
DataTable filtered = dt.Clone(); 

//fill the clone with the filtered rows 
foreach (DataRowView drv in dt.DefaultView) 
{ 
    filtered.Rows.Add(drv.Row.ItemArray); 
} 
dt = filtered; 

pero que la solución era muy lento, incluso para sólo 1000 filas.

La solución que funcionó para mí es:

//create a DataTable from the filtered DataView 
DataTable filtered = dt.DefaultView.ToTable(); 

//loop through the columns of the source table and copy the expression to the new table 
foreach (DataColumn dc in dt.Columns) 
{ 
    if (dc.Expression != "") 
    { 
     filtered.Columns[dc.ColumnName].Expression = dc.Expression; 
    } 
} 
dt = filtered; 
Cuestiones relacionadas