How to transform DataRow to DataTable

studio
variable

#1

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


#2

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


#3

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!


#4

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:


#5

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


#6

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


#7

Did you try below or passed row?

datatable - dt2
Arrayrow -dt1row.ItemArray


#8

Thanks man! It worked!!!


#9

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?


#10

Did you try

DT = DR.CopyToDataTable


#11

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


#12

DataRow is a .Net structure - check MSDN:
https://msdn.microsoft.com/en-us/library/system.data.datarow(v=vs.110).aspx


#13

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”.


#14

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


#15

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.


#16

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:


#17

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.


#18

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?


#19

I’m only aware of this happening with System.Data.DataSetExtensions.dll.
Affected methods are here on MSDN:
https://msdn.microsoft.com/en-us/library/system.data.datatableextensions_methods(v=vs.110).aspx

Basically it’s .AsEnumerable(), .CopyToDataTable() and .AsDataView() (and their overloads).


#20

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…!!