How to get column names of data table to an array?

I have tried so many things to make this seemingly simple task work :slight_smile:

How do I store the column names of a data table to an array? The array is to be used for the drop-down input dialog options. I can get the column values to an array, the row values to an array, but the column names I just can’t seem to get.

2 Likes

Use for each

For each item in datatable.columns

And for each argument type is system.data.datacolumn

Use an assign statement inside for each and use
Strcolumnnames = strcolumnnames+" "+item.columnname

After the for each ends you have a variable with all column names, now you can split this strcolumnnames using space and you get the array.

I get several compile errors using your code:
Option strict on disallows late binding
Operator + is not defined for types “System.Data.Datacolumn” and “string”

The loop way might work, and that is the most important thing, but I was hoping for a more sexy solution like the code one below that returns column values as an array. The best solution for me would to have a similar code that returns an array with the column names not the column values. The code itself will work if I store the column names as values in a column in a data table and then retrieves the column names with the code below, but that do not seem efficient to me.

(From row in DT.DefaultView.ToTable(true, “ColumnName”).AsEnumerable() Select Convert.Tostring(row(“ColumnName”))).ToArray()

Use item.columnname.tostring

Hi @Nekrom

Refer the below linq query to extract column names from datatable

(From dc In dt.Columns.Cast(Of DataColumn)
Select dc.ColumnName).ToArray()

Refer the below workflow.

Sequence.xaml (7.3 KB)

7 Likes

Hi @Nekrom,

Refer the below workflow with few updates

Sequence.xaml (7.0 KB)

2 Likes

Fantastic! Thank you! :smile:

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