How to add the last 3 rows value of datatable in dictionary

Hello All,
I have one column , suppose column A

I want to want the last three rows values and their corresponding row number in dictionary.
Please help

Get Last Three Rows.zip (8.0 KB)
Hi Karan, please find the attached file.

1 Like

which activity you have used here?

Add to Dictionary

could you please send me the screenshot for that?

Here the value I want to add where , key will be index that is integer , value will be string

Hi
Hope these steps would help you resolve this
—hope we have the datatable ready obtained from read range activity named dt
—now create a dictionary variable in variable panel Named dic_input and with default value as new dictionary (of string, string)

—use a for each row loop and pass the variable dt as input
—inside the loop use a IF condition like this
dt.Rows.Count-3 <= counter
If true it will go to the THEN part where we can have a assign activity like this
dic_input(dt.Rows.IndexOf(row).ToString) = row(“yourcolumnname”).ToString

Where counter is a int32 variable defined in the variable panel with default value as 0
This will add the row index and its value to the dictionary
—still being inside the for each row loop use a assign activity next to the IF condition like this
counter = counter + 1

Hope this would help you
Cheers @Karan28

1 Like

here the value which I have to add is , row number and row value

for example - column 0
1 Karan
2 Arora

here the value should be saved as - key -2 , value -arora

You can change the dictionary to dictionary(of Integer, string)
And pass the row index as key and value as value

Thanyou , Could you explain me this part
dic_input(dt.Rows.IndexOf(row).ToString) = row(“yourcolumnname”).ToString

because I have only one column in my datatable , whose column index will be 0

1 Like

Sure
The left part is we try to create the key in dictionary for the value we are going to insert
And for that we need row index which is give by dt.Rows.IndexOf(row)
And by mentioning like this we can create a key for the value
dic_input(dt.Rows.IndexOf(row).ToString)

And then the right part of assign activity is the value for the key
And if we have column index no worries we can mention like this
row(0).ToString

Cheers @Karan28

we are not using add to dictionary activity here ?

1 Like

yah thats another way of doing
and this is another way of doing…and this is what is even followed in REFramework Template

Cheers @Karan28

yes I got that , thanyou for your time but one last thing

dt.Rows.Count >= counter-3

this part is not exactly working here
and there is one error while assigning

Fine
If the key of the dictionary is a integer then remove .ToString in this expression

Cheers @Karan28

thankyou for this , just a last ques

how to get the value on the basis of key in message box just to check if the value inserted or not?

1 Like

yah like this
yourdictionaryvariablename(“Key”).ToString

Cheers @Karan28

but here in this case what will be the value of key?