How do I add a new column to my data table

Hello

I have a data table and I would like to add a new column. I’d like to use add data column activity however I’m not sure how do I insert this new column as a first column.

I’d like the new column to be first in my data table. How do I move it there?

Thank you.

Hi @marcin.chowaniec ,

Maybe you could check the below post :

Could you please help me out with this? I don’t quite understand. I should use activity Invoke Method and then how do I choose the column which I’d like to be first?

So this is what I have:

I add a column to my data table.
and then I go with Invoke method, and now how do I point out that the last column in my data table that was added with the previous activity, should be moved before all of the columns so it be first on the list.

Hi @marcin.chowaniec

Use invoke method activity:
yourDatatable.Columns("columnName").SetOrdinal(0)

In/Out: yourDatatable

Hello :slight_smile:

So is it Invoke Code or Invoke Method?

@marcin.chowaniec

try with invoke code

using the below expression

dt.Columns.Add(“new”, GetType(System.String)).SetOrdinal(0)


This is error I’m getting. Any ideas?

@marcin.chowaniec

That’s invoke code.

If you only have invoke method then:

  1. In the TargetType property, select DataTable.
  2. In the MethodName property, enter “Columns”.
  3. In the TargetObject property, assign your DataTable.
  4. In the Parameters property, new Object() { “NewColumnName”, 0 }
1 Like

So this will add new column then? I would not have to use add column activity before is that correct?

@marcin.chowaniec

work_database_tofilter.Columns(“Date:”).SetOrdinal(0)

@marcin.chowaniec

No this code will only change the position of column. Yeah you need to add the column first.

You said that you have already added the column.

Thank you, I will try this now.

Try this for both add column and position change @marcin.chowaniec

Dim newColumn As New System.Data.DataColumn("Date:")
work_database_tofilter.Columns.Add(newColumn)
work_database_tofilter.Columns("Date:").SetOrdinal(0)

or

work_database_tofilter.Columns.Add("Date:").SetOrdinal(0)

I would like to thank you very much. I will save your answer so that it won’t cause me any trouble in the future. Thank you, you have been a great helper!:slight_smile:

1 Like

In UiPath, when you use the Add Data Column activity, the new column is by default added at the end of the DataTable. To add the new column as the first column in your DataTable, you can follow these steps:

  1. Add Data Column:
    Use the Add Data Column activity to add the new column at the end of the DataTable. Assign a default value, if necessary.

  2. Reorder Columns:
    After adding the column, you can use the Assign activity to reorder the columns in the DataTable. To move the newly added column to the first position, you can use the following expression in the Assign activity:

    YourDataTable = YourDataTable.Columns.Cast(Of DataColumn)().OrderBy(Function(col) If(col.ColumnName = "NewColumnName", 0, 1)).CopyToDataTable()
    

    Replace YourDataTable with the name of your DataTable and "NewColumnName" with the actual name of the column you want to move to the first position.

    This expression orders the columns based on whether the column name matches the specified name. The column with the specified name will have an index of 0 and will be positioned first in the DataTable.

Here’s how you can implement this in UiPath:

Add Data Column (Add your new column at the end of the DataTable)

Assign Activity:
YourDataTable = YourDataTable.Columns.Cast(Of DataColumn)().OrderBy(Function(col) If(col.ColumnName = "NewColumnName", 0, 1)).CopyToDataTable()

After executing these steps, your new column will be the first column in the DataTable.

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