How to make second row of a datatable as header

Hi,
I have filtered DataTables
example
Product Detais, Column1, Column2
Name,age,Gender,xyz
nnnn,23,M,xxxx
kkkk,20,f,yyyy

I have such filtered datatables taking commonly Product details as header
I want to change such datatables second row as headers

ex:
Product Detais, Column1, Column2
Name,age,Gender,xyz
nnnn,23,M,xxxx
kkkk,20,f,yyyy

Can anyone help

Hi,

How about the following steps?

  1. Convert your datatable to string using Output Datatable activity.
  2. Remove first line using regex.Replace method or Replace activity.
  3. Convert the string to datatable using Generate Datatable activity with Csv Parse option and UseColumnHeader option.

img20200827-1

Regards,

1 Like

This can be done quickly and easily with a simple ‘for each’ loop. I will assume your datatable is a variable called dt1

Use a for each activity and change the TypeArgument to System.Data.DataColumn
For each col in dt1.Columns
Assign col.ColumnName = dt1.rows(1).item(col).ToString

2 Likes

Hey Yoichi,

You beat me to it :slight_smile:
I did exactly the same as you and came here to post. no point now.

The only thing that confused me is using the UseColumnHeader option, to me it seems logical to use the UseRowHeader option but it was erroring for me so tried Column header and it worked.

image

1 Like

Hi @roy_r ,

Thank you for pointing. It was my mistake. We should check not UseColumnHeader but UseRowHeader.

Regards,

1 Like

Hey Yoichi,

No You were CORRECT using UseColumnHeader is the one that works, UseRowHeader faults.

I think this might be a bug in UiPath where they have got the wording the wrong way around?

1 Like

Hi,

Thank you for you reply. I actually checked behavior of UseRowHeader and UseColumnheader.

I confirmed UseColumnHeader is correct in this case. (Sorry for confusion.)
However, I cannot reproduce any error in UseRowHeader. Can you share your data?

Regards,

Hey Yoichi,

That’s interesting that you don’t get an error when using UseRowHeader.

image

This is what I get, If I use UseRowHeader,

Interestingly it doesn;t error If I just use row(0) then at least something is returned which is the second column? which also seems wrong.

Userowheader is working fine for me

Maybe my machine needs a rebuild :-S

Hi @roy_r ,

If we check UseRowHeader option, the activity considers the first column as a header and delete it.
So if table string has 2 columns, result table has just a column because the above specification.
This is why your error occurred.

Regards,

1 Like

That makes sense that it is deleting it - thank you now I understand why it was erroring. Just logically in terms of the English language I would have thought UseRowHeader would mean use the row as the header. Anyway Nikhil is sorted now. and thanks for the explanation that the column was being deleted.

2 Likes

@roy_r @Dave @Yoichi thanks for support

1 Like

Hi, @Yoichi. I used this exact method, but somehow it didn’t delete the first row. Just to check, was this the code in the image?

System.Text.RegularExpressions.Regex.Replace(text,"^.*¥n","")

Regards,
Yachee