Select column from datatable

How can I select a specific column from a datatable.

I tired dt.select(“[ColumnName]”).CopyToDataTable but it doesn’t work

You can iterate by for each row loop and row(“column name”) gives the data in column

Can I get the entire coumn as datatable without looping ?

Yes. By using filter data column just use the column name in output column and the output will be in data table variable

assign this to a new Datatable variable

image

new DataView(dt).ToTable(false,new List(Of String)({"ColumnName"}).ToArray)

to select multiple columns (ColumnName1 and ColumnName2) to new DT
new DataView(dt).ToTable(false,new List(Of String)({"ColumnName","ColumnName2"}).ToArray)

2 Likes

Hi Jack,
thank you, it works! I have some questions:

How would the code be with column index instead of column name ? And how can I remove the headers ?

no problem :slight_smile:

you can do something like this

new DataView(dt).ToTable(false,new List(Of String)({dt.Columns(0).ColumnName}).ToArray)

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.