Datatable delete lines

How to delete the first 3 lines from a datatable
So the first 3 lines should include the column as well

@Ritaman_Baral

dt = dt.AsEnumerable().Skip(3).CopyToDataTable()

Hi @Ritaman_Baral

  1. Read Range Activity: Read data from Excel/CSV and store it in DataTable variable (e.g., dtData).

  2. Assign Activity:

    • Create a new DataTable variable (e.g., dtWithoutFirstThreeRows).
    • Use the following expression to skip the first three rows:
      dtWithoutFirstThreeRows = dtData.AsEnumerable().Skip(3).CopyToDataTable()
  3. Use dtWithoutFirstThreeRows in your subsequent activities as needed.

You can remove the first 3 rows (including the header) from a DataTable in UiPath using the DataTable.Clone method along with the DataTable.Rows.RemoveAt method. Here’s an example of how you can achieve this in UiPath:
’ Assuming dt is your DataTable
Dim dt As DataTable = yourDataTableVariable

’ Check if the DataTable has at least 3 rows
If dt.Rows.Count >= 3 Then
’ Create a new DataTable with the same structure (columns)
Dim newDataTable As DataTable = dt.Clone()

' Add rows from the original DataTable, excluding the first 3 rows
For rowIndex As Integer = 3 To dt.Rows.Count - 1
    newDataTable.ImportRow(dt.Rows(rowIndex))
Next

' Now, newDataTable contains the original DataTable without the first 3 rows
' You can use newDataTable in your workflow as needed

' If you want to update the original DataTable with the modified one, you can do:
dt = newDataTable

Else
’ Handle the case where there are fewer than 3 rows in the DataTable
LogMessage(“The DataTable has less than 3 rows. No rows removed.”)
End If

After this process, newDataTable contains the original DataTable without the first 3 rows. If you want to update the original DataTable, you can assign the newDataTable back to the original DataTable variable (dt ).

I am reading a excel
In the excel

  1. First line contains JARGON DATA 1
  2. Second line contains JARGON DATA
  3. third line contains JARGON DATA
  4. 4th line contains original column names

I want the 4th line to be my column and the rest will be data

It is not skipping the first line

@Ritaman_Baral

dt = dt.AsEnumerable().Skip(1).Skip(2).CopyToDataTable()
or
dt = dt.AsEnumerable().Skip(3).Select(Function(row) row).CopyToDataTable()

Hi @Ritaman_Baral

Try this

DT.AsEnumerable().Skip(3).CopyToDataTable()

Input:

image

Output:

image

Note: Uncheck Add Headers option in both Read Range and Write Range activities

Regards,

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