Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy specific columns from one DataTable to another

Tags:

c#

.net

datatable

Have some read in data (from excel file) in a DataTable and now I want to filter this and copy only specific columns to the other one!

dataTable format:

some data 
ColA|ColB|ColC
xxxx|xxxx|xxxx
some data

some data represents other table data not related to ColA-ColC

How can I copy ColA-ColC with xxxx to the new DataTable?

Thx

like image 275
leon22 Avatar asked Aug 23 '13 12:08

leon22


1 Answers

You can simply do it by using DataView.ToTable() :

System.Data.DataView view = new System.Data.DataView(yourOriginalTable);
System.Data.DataTable selected = 
        view.ToTable("Selected", false, "col1", "col2", "col6", "col7", "col3");
like image 111
Arshad Avatar answered Oct 22 '22 13:10

Arshad