How to get column index?

I can look up the column name as below
DT.Select(“[math]=‘Pass’”).Count

But I want to find the column index
Tell me how to do it

Do you know the column name before whose index you want?

You have to use for each row, then make a counter and increase it in each loop.

KR,

Pablo

1 Like

@Sunny
DT.Columns(“[ColumnName]”).Ordinal will give you an integer value representing the column index

9 Likes

Hi @SunnyK,

Welcome to the community!

If you do not know the column names, you can always loop through the Columns to get the name and index. Sample give below.

ColumnIndex.xaml (6.6 KB)

1 Like

@SunnyK

If you the column name use this

DataTable.Columns.IndexOf(“ColumnName”).ToString

8 Likes

Hi Sir,
I using the method you mention but I come out -1 as the value. Why is this result?

Thank You!

1 Like

That is probably because the column name you are passing does not exist.
The difference is:
When you use yourDatatable.Column.IndexOf(“ColumnName”), if the column name does not exist in that databale, the output is -1

And when you use yourDatatabel.Column(“ColumnName”).Ordinal, if the column name does not exist, it throws an exception.

But both ways are OK to get the index of a particular column by its name.

Cheers.