Find column index of a column that contains keyword

hi guys, i want to iterate through columns which name contains a certain keyword. i think the best way to do this is by using for each row and use row([column_index]). But how can i find the column index ?

1 Like

Hey @RobertoEwaldo,

You are halfway.

  1. Iterate through for-each row
  2. Have one more for-each inside to iterate through columns of every row
  3. Add a if condition to check it contains keyword and if condition passed store the index value you get from for-each

Hope this helps.

Thanks :slight_smile:

hi @Nithinkrishna,

the keyword is only on the column header

1 Like

dt.Columns.IndexOf(“Gender”)

image

image

image

1 Like

hi @ImPratham45,

the column header is not exactly “gender”, something like “aa_gender”, and there is another excel file with column header “bb_gender”. i want to get the index of the columns that contains gender

something like " * gender * "

Do like this

1 Like

@RobertoEwaldo
give a try on:

Assign activity:
leftside: ColIndex - datatype: Int32
right side:

dtData.Columns.Cast(Of DataColumn).ToList().FindIndex(Function (x) x.ColumnName.toUpper.Contains("GENDER"))

this works, thanks !

Most Welcome!!! :blush:

this works too, but what if there are more than 1 column that contains the keyword ?

it finds first occurence
for getting all indexes we can reformulate. Any specific need like index and found full column name for all columns containing “XYZ”?

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