Friday, March 11, 2011

Copy Pecific Columns(With Data) of One DataTable To Unother DataTable

Copy Pecific Columns(With Data) of One DataTable To Unother DataTable

There are two datatable DT1 And DT2

DT1 Consists of below 5 Coloumns:

1. EmpID
2. EmpName
3. Department
4. Designation
5. Reporting_Office

Now I Want to Copy Only below 3 columns data to DT2

1. EmpID
2. EmpName
3. Department

Code :

// Create Array with required coloumns Name
string[] cols = {"EmpID","EmpName","Designation"};

// Create new Datatable
DataTable DT2 = new DataTable();

// Fill datatable with new colunms data
DT2 = DT1.DefaultView.ToTable("DT2", false, cols);

No comments:

Post a Comment