Copy the Sorted Dataview To DataTable in .Net 1.1
The below code can be used to copy the sorted DataView object to a DataTable object.
dt=dv.Table.Clone();
// Clones the structure of table
int i = 0;
string [] ColNames = new string[dt.Columns.Count];
foreach (DataColumn col in dt.Columns)
{
ColNames[i++] = col.ColumnName;
//Getting the column names to a string Array
}
IEnumerator Ienum = dv.GetEnumerator();
// Get the enumerator that can iterate through a collection
while (enum.MoveNext())
{
DataRowView drv = (DataRowView)Ienum.Current;
// Gets the Current element in the collection,ie.DataRowView
DataRow dr = dt.NewRow();
try
{
foreach (string strName in ColNames)
{
dr[strName] = drv[strName];
// Copy the data to Datatable
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
dt.Rows.Add(dr);
}
In the above code, dv contains the sorted DataView object.
|