How to trim whitespaces from column names in datatable

I have a datatable which has leading and trailing whitespaces. I want to remove them but not the ones in the middle.
For eg: " Column 1 " to “Column 1”

@jamnanin

try this
ListA=(From p in dt.columns.CastOf(System.Data.DataColumns)
Select Convert.ToString( p ).Trim).ToList

You will get the column names without spaces.

Regards,
Mahesh

Hi @MAHESH1 : Thanks but I am reading an excel file then inserting a For Each Row loop and inside the loop, applying an IF condition, pl. see below: I only want to trim the column name in IF condition, how should I do that:

If CurrentRow(“Turn Over - ZNA”).Equals(“”) Then …

so should I trim the column name in IF condition or just after reading the excel file with Read Range Activity? Below is the screenshot:

Hi @jeevith : I am reading an excel file then inserting a For Each Row loop and inside the loop, applying an IF condition, pl. see below: I only want to trim the column name in IF condition, how should I do that:

If CurrentRow(“Credit Turn Over - ZNA”).Equals(“”) Then …

Here is the column name is “Credit Turn Over - ZNA” and it has a leading and trailing white spaces in excel. So, I want to remove before proceeding further.

Hi @Dr_Raza_Abidi_PhD ! :grinning_face_with_smiling_eyes:

I think that the best strategy would be that just after the read range, you Trim all the column names.
Here is a suggestion to do so: Remove spaces in headers.xaml (8.5 KB)
Beware: this option won’t work if you have 2 identical column names (after the trim)

Let us know if it does not work as expected :grin:

@Hiba_B : Thank you very much for your swift response. I have tried the same but in the assign activity, it does not select the ColumnName. Here is the screenshot:

image

It does not give show the col.ColumnName method in the list.

Hi @Dr_Raza_Abidi_PhD ,

Could you select the TypeArgument to System.Data.Datacolumn to resolve the issue. thanks.

Hi !
In the foreach properties, change the type argument to system.data.datacolumn: then it will appear :wink:

item.Columnname = itemColumnname.trim()

Dont forget to configure the typeargument of the for each to the datatype System.Data.Datacolumn

Refer the steps highlighted by @kirankumar.mahanthi1

Thank you.

Thank you so much @kirankumar.mahanthi1 : Now the code is running but the following error occurred during execution.

@Hiba_B : Thank you so much but the following error occurred:

@Jobin_Joy : Thank you very much Jobin.

Still facing issues on this? Thank you.

Hi @Dr_Raza_Abidi_PhD ,

If possible could you pls share your work flow. thanks.

@kirankumar.mahanthi1 : Here is the screenshots:

@Jobin_Joy : Yes still facing the issue

Could you pls mention which activity it is throwing the mentioned error to troubleshoot to further. thanks.

For Each Activity is throwing exception where I am removing whitespaces from the headers of columns. Thanks,

Hi @Dr_Raza_Abidi_PhD

Can you please debug the code little more deeper? the code is working for me,

image


image

Thank you.

@Jobin_Joy : Pl. find below the screenshot: In my excel file, there is a column highlighted in yellow, throwing an exception. Pl. guide what to do?? Many thanks,