How to Find a string in Datatable

Hi all,

I am having a string value
Str=abcd

Now how to find this value in the column A of DT1.
If present in the Column A then get the position of the row in which this value exists.

Can any one help me out with this.

thanks in Advance.

Sonika

Hi,

1.read range take output as dt
2.for each row
3. Use If condition (String = row(0).tostring)

Refer this link for detailed answer

@Lawrance_A

I went through the link .
I found they are going through all the columns… But i want to go through only one particular column in DT1
image

And further code i dint get where they are finding the row position :frowning:

i guess here they are finding the same string…
image
But where they are finding the row position??

i went through

Please guide me.

Regards,
Sonika

Hi,

Here is a more straightforward approach, handling the situation the row would not exist
It is based on the IndexOf and Datatable Select, that you surely encountered on the Academy training

Assign RowPostion = dt.Rows.IndexOf(dt.Select(“[ColumnA] = ‘abcd’”).FirstOrDefault())

Some side notes:

  • The position is effectively 0 based, so if the occurrence appears in the first row, the position returned will be 0, on the third: 2.

  • If the value appears to be not found in the column (Name specific inside the select string within Brackets, it will return the value -1.

  • The returned value is an Int32, so feel free to add +1 to it,or/and print it in your logs using RowPosition.ToString

  • It will return you the first occurence in the columnn, meaning that if the values are both found at index 5 and 15, the 5 should be returned. If you need the last occurence, You Can Replace ‘FirstOrDefault’ by ‘LastOrDefault’ in the expression.

Cheers

2 Likes

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