Home > Prsnl References > Copy a column Data Of Datatables using C# to another DataTable

Copy a column Data Of Datatables using C# to another DataTable


DataTable dtData1 = new DataTable();
//write code to fill this datatable

DataTable dtData2 = new DataTable();
//create structre and data of datatable1 to datatable2
dtData2 = dtData1.Copy();

//remove unwanted columns.If the column name is not known u can use the column no
dtData2.Columns.Remove("Col1");
dtData2.Columns.Remove("Col3");
dtData2.Columns.Remove("Col5");
dtData2.Columns.Remove("Col7");

//Rename the Columns to the new names if needed
dtData2.Columns["Col2"].ColumnName = "Col1";
dtData2.Columns["Col4"].ColumnName = "Col2";
dtData2.Columns["Col6"].ColumnName = "Col3";

//save changes
dtData2.AcceptChanges();

Advertisements
Categories: Prsnl References
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: