I have trimmed the headers of the columns in the whole data table but how to trim the white spaces in the cell values of the columns for the whole data table? Pl see the screenshot:
Column Headers are successfully trimmed but I want to trim the whole data table cells values also.
Instead of that expression you can try with this one
(from r in DataDT.AsEnumerable
Select ia = r.ItemArray.toList
Select ic = ia.ConvertAll(Function (e) e.ToString.Trim()).toArray()
Select GoodDT.Rows.Add(ic)).CopyToDataTable()
@Gokul001 : The code file .xaml shared by @Jobin_Joy was perfectly okay with the all data table rows and columns. I am only trying to do trimming the values on single column.
I have shared the .xaml file in which I am trying on single column and it is giving an error. For all columns and rows the code is fine.
(From r In dtData.AsEnumerable
Select rac = r.ItemArray.Select(Function (x) x(“Column Name”).toString.Trim).toArray
Select dtGood.Rows.Add(rac)).CopyToDataTable()
This above code when I place my variable, it gives me an error:
I agree with @Jobin_Joy a simple for each row activity will help you here no need to fuss with linq. If you are having problems understanding the code, then others who have to maintain it might also have it. I am not against linq, but I am for keeping the code simple and easily maintainable.
Use the solution suggested by @Srini84. That is all you need.
For each row in YOURDATATABLE
# Use assign activity
row("YourColumnName") = row("YourColumnName").ToString.Trim
That is it.
What does the above do.
On the left hand side is the row(“YourColumnName”) object
On the right hand side converts that object to string and trims empty spaces.
YOURDATATABLE will then have a column which is free of spaces (start and end), spaces in the middle of string will remain.
Trim for all columns
Sooner or later you will want to extend this logic to cover all columns in your datatable.
To make the above code even more reusable and cover all your columns, you can have an outer For each loop and iterate through YourDatatable.Columns which is of an array type.
For each column in YOURDATATABLE.Columns
For each row in YOURDATATABLE
# Use assign activity
row(column.ToString) = row(column.ToString).ToString.Trim