.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Collections >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Copying DataTable

Posted By:nishithraj       Posted Date: December 27, 2009    Points: 10    Category: Collections    URL: http://www.dotnetspark.com  
 

There are different scenarios to copy data from one data table to another.

We can do as follows.

DataTable DataTable1= reader.GetSchemaTable();

dtSchema = new DataTable(m_strName);
dtSchema.Columns.Add("ColumnName", typeof(String));
dtSchema.Columns.Add("ColumnOrdinal", typeof(Int32));
dtSchema.Columns.Add("ColumnSize", typeof(Int32));
dtSchema.Columns.Add("DataType", typeof(String));

foreach(DataRow dr in dtTemp.Rows)
{
DataRow drNew = dtSchema.NewRow();
drNew[0] = dr[0];
drNew[1] = dr[1];
drNew[2] = dr[2];
drNew[3] = dr[3];
dtSchema.Rows.Add(drNew);
}

But this may affect the perfomance of the application, the easiest way to achieve this is

Copy method of DataTable as follows.

DataTable2= DataTable1.Copy();

We can remove a specific column too by this method.

DataTable2.Columns.Remove("ColumnToRemove");

 


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend