How to get the row index through selecting another datatable?

Hi everyone,
I want to add two data column to a datatable from another one like this.
sample_datatable

My aproach is here.
Step1. Use “For each row” activity with dt2 and get dt2.row(0) and dt2.row(1).
Step2. Select dt1 and get row index.
Step3. Use “Add data column” activity and add dt1.row(2) and dt1.row(3).
Step4. Assign dt2.row(0) to dt1.row(2) or (3). (this step will also use the same method)

I have no idea of Step2 method.

Best regards.

hi @ak_mariam

Check the below post

Thanks
Ashwin S

Hi @ak_mariam,
I have created the sample with ur datasource. Below I have attached the files.

File : get the row index through selecting another datatable.zip (21.5 KB)

image

Regards
Balamurugan

Hey @ak_mariam

You can get Row index from datatable inside Loop like below :slight_smile:

foreach(DataRow row in yourdatatable.Rows)
{
Int value_row_index =yourdatatable.Rows.Indexof(row)
//Writeline value_row_index.ToString()
}

Regards…!!
Aksh

2 Likes

Hello @aksh1yadav,
just correctness
That gives an error, we have to use only like,
foreach(DataRow row in yourdatatable) {
}

Thanks,
Pankaj

Hey @Pankaj.Patil

What error?

Mate for your clarification This is for Generic For each where you have to use System.Data.Datarow As an argument and in For each value you have to use Datatable.Rows :slight_smile:

Since i used it in C# so wrote like that.

Even if you will use For Each Row Activity from UiPath , yourdatatable.Rows.Indexof(row) this will be same :slight_smile: So you do not have to worry about for each construct i have mentioned above :slight_smile:

Make sense? @Pankaj.Patil

Regards…!!
Aksh

Hello @aksh1yadav,

I was talking about For Each Row activity, you were only For Each.
That was confusion between us.

Thanks,
Pankaj

I know that is why explained :slight_smile: mate

Regards…!!
Aksh

@Pankaj.Patil @aksh1yadav
Thank you for your answer.I could resolve the probrem.

I use the same way but it gave me error as For each row : Object reference not set to an instance of an object.

How can I put the index in my write cell, cell field? :frowning:

1 Like

Could you share your workflow file?

Hi @ak_mariam,

Thanks, attached the workflow.

ForTesting.xaml (8.5 KB)

The usage of Rows.Indexof (row) is correct, but some errors has occurred elsewhere.

  1. Please add “dt” to the output field of the Read range activity.
    2 .and change the cell field of the Write cell activity from “B” +index.ToString to “B” +(index+1).ToString.
    try it.
1 Like

Yes I got it. We have 2 plus two. Thanks @ak_mariam

1 Like