CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

Copy the Sorted/Filtered Dataview To DataTable in .Net 1.1
Submitted By Satheesh Babu B
On 12/8/2008 8:47:12 AM
Tags: C#,CodeDigest  

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.

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..