How to transform DataRow to DataTable

I would like to write just one row of a data table in excel without going item by item of the row. I think I should transform the dataRow into a dataTable in order to write it, so how could i perform this change?
Thank you

1 Like

hey @Briongos

You can use **build Datatable ** Activity and then by using Add data row Activity just add that row into that particular datatable and then you will be able to write that particular data row into a datatable.

Regardsā€¦!!
Aksh

1 Like

Thanks @aksh1yadav!

I already tried to do it that way, however, it didnā€™t work because the datatable i created is empty with no rows and columns as i donā€™t know the number of columns that the row could have.

Thanks!

1 Like

In that case you can use add data column activity and can add column if you are getting it dynamiclly then by couting the row and using counter add data column one , two like so into the blank datatable.:wink:

2 Likes

You could clone your Source Datatable where you got the row from and then perform Add Data Row (or Import Row) to the new DataTable.

dt2 = dt1.Clone

add data row ā†’ datatable - dt2 rowarray -dt1row.ItemArray

2 Likes

Thanks for your help!

I tried this way but an error occured saying ā€œThis row already belongs to another tableā€.
What Iā€™m currently doing is getting a data table from excel and going through the table with a for each row and i want to copy the row to another table if the value of a column is for example higher than X.

Cheers

Did you try below or passed row?

datatable - dt2
Arrayrow -dt1row.ItemArray

5 Likes

Thanks man! It worked!!!

Hey,
I have the same question, but with a DataRow (letā€™s say DR).
I use a ā€œAdd data rowā€ activity for convert my DR to a DataTable (DT).

ArrayRow - DR
DataRow
DataTable - DT

But DR have 3 rows and DT have only one. Someone know how to fix it?

1 Like

Did you try

DT = DR.CopyToDataTable

7 Likes

Thanks so much, it works!
Could you tell me where I can get the list of method for dataRow in UiPath, please?

DataRow is a .Net structure - check MSDN:

HI,
I am having a and Array of DataRows but copytoDataTable function is not available.

I tried clone method to create new data table and add individual row, but getting ā€œThis row already belongs to another tableā€.

Hey @ezharul.ansari

CopyToDataTable() only works when your query returns an IEnumerable<System.Data.DataRow>

Like this:

IEnumerable<System.Data.DataRow> new_dt = dataTable.AsEnumerable().CopyToDataTable()

Regardsā€¦!!
AKsh

An Array<T> is an IEnumerable<T>, since all arrays (and most other collections available in system.collections and system.collections.generic) implement the IEnumerable (or IEnumerable<T>) interface. You donā€™t need to cast a class that implements an interface to call a method inherited from that interface.
So a DataRow[] is an IEnumerable<DataRow> by default.

Did you try just typing it in?
Sometimes intellisense (autocompletion) doesnā€™t kick in the first time, but the workflow should work correctly.

1 Like

Yes u r correct. I just was aware some day back with this fact when i was just Reading about it and intellisense will not give error with this too but really strange that day it was returning error so i was not able to execute but now its works.

It works:+1: Intellisense :frowning:

The CopyToDataTable, as well as a couple of other useful ones (like .AsEnumerable()) are in a separate .dll.
There were some issues in the past that this .dll didnā€™t load correctly until it was referenced, you mightā€™ve stumbled upon that just now.

Yeah seems like that that is bcz when it does not work for me in past i started always to prefer Ienumerable data row collection.
So such probs can exists with others utility functions as well? @andrzej.kniola?

Iā€™m only aware of this happening with System.Data.DataSetExtensions.dll.
Affected methods are here on MSDN:

Basically itā€™s .AsEnumerable(), .CopyToDataTable() and .AsDataView() (and their overloads).

2 Likes

I have faced some in the past but solved by referencing but

CopyToDataTable<T>(IEnumerable<T>)	
Returns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter T is DataRow.

Regardsā€¦!!

1 Like