Linq Filter DataTable and rename Columns

Hi, i struggle whit rename different columns.
I would like to name the columns 1-7 specifically how do I achieve this?

(From d In DT_aus_PDF.AsEnumerable
Where System.Text.RegularExpressions.Regex.IsMatch(d(“Column2”).ToString,"([0-9]{8})")
Where Not d(“Column3”).ToString=""
Select d).CopyToDataTable().DefaultView.ToTable(False, “Column1”,“Column2”,“Column3”,“Column4”,“Column5”,“Column6”,“Column7”)

Hi @NHoe ,

Would you be so kind as to explain a bit more on the operation you are trying to perform?
You want to rename the first seven columns, but to what?

I would suggest using a For Each Activity and pass in an Array of the Columns like so:

Dt.Columns.Cast(Of DataColumn).Select(Function(s) s.ColumnName).Take(7).ToArray()

Then assign the names one by one->

Dt.Columns(col).ColumnName = "Column"+(int_index+1).ToString

RenameFirstSevenColumns.xaml (8.8 KB)

Kind Regards,
Ashwin A.K

Hi, the table is from a PDF, in it the value is only as a string and is converted by generate Data Table. Then adjusted via the Linq command so that the output format is correct.

The only thing missing are the correct column headings, as these are only output as default values.

Hi @NHoe ,

So are you saying that you want to rename all the column names generically like Column1, Column2…Columnn?

Maybe you could provide some screenshots if possible so that we are on the same page?

Kind Regards,
Ashwin A.K

Exactly, I want to rename all columns from 1 to 7. For example, column 1 = number , column 2 = description , column 3 = price

Hi @NHoe ,

Sure, that is possible as long as you have an Array/List of the new column names.
You can loop through them and assign the new names to the Columns →

image

image

Dt.columns(Dt.Columns(int_index).ColumnName).ColumnName = col

Kind Regards,
Ashwin A.K

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.