Datatables

Hi,

I have a CSV file that has 550 columns I want split this to 10 tables. How can we create multiple data tables from single table. Please help

Hi @ssk,

it depends on how you want them split? Do these need to split by certain row increments or by column increments.

e.g. columns A - D and then D-F etc OR all columns but rows 1-10 and then 11-20 etc.?

Either way I would possibly using a looped read range to read into separate datatables.

A brief google for “VBnet split datatables” shows several possible answers if you are looking for a code answer.

Thanks

You can place an if condition with rows.count<50,<100 … and build datatable with those values
@ssk

Hi @ssk,

Refer this link

private static List<DataTable> SplitTable(DataTable originalTable, int batchSize)
{
     List<DataTable> tables = new List<DataTable>();
     int i = 0;
     int j = 1;
    DataTable newDt = originalTable.Clone();
   newDt.TableName = "Table_" + j;
   newDt.Clear();
    foreach (DataRow row in originalTable.Rows)
    {
         DataRow newRow = newDt.NewRow();
         newRow.ItemArray = row.ItemArray;
         newDt.Rows.Add(newRow);
         i++;
         if (i == batchSize)
        {
           tables.Add(newDt);
           j++;
          newDt = originalTable.Clone();
          newDt.TableName = "Table_" + j;
          newDt.Clear();
          i = 0;
      }
  }
   return tables;
}

Regards,
Arivu

Hi
I want to split by 50 columns in each data table and I want to do this in UiPath

Thanks for your reply. I want to do this in UiPath

Thanks for reply. I want split in UiPath by column and rows. Like 50 columns and all rows for that columns in a data table.

I have given the process to do itin uipath only using FOr each row,if condition,builddatatable

Hi @ssk,

Yes Its possible follow the same steps in the uipath.

Regards,
Arivu

Thanks again. I am new to UiPath so finding it difficult.

With builddata table I was able to create a table with 7 columns but adding data to those columns from source which has 550 columns is the issue. If I use for each row it will get a row will all 550 columns instead of 7 column rows.

Thanks again. I am new to UiPath so finding it difficult to put same thing in UiPath code. I tried using select for getting only few columns but that returns DataRow() and not sure how to declare the DataRow array

@ssk

Let us take your datatable as dta.
Let us take List of Columns Names List A

ListA=(From p in dta.Columns.CastOf(System.Data.DataColumns)
Select Convert.ToString(p.ColumnName)).ToList

Then Initialise two Integer Variables
intn=0
intk =1

Now You take the inta =ListA.Count/Int Tables
int Tables The number of Tables you want to Create (For your scenario it will be 10)
Create one more datatable dtb
Then Run While loop by giving Condition(K<=int Tables)
Inside while loop
add one if condition
IsNothing(dtb)
If False Then use Clear DataTable to clear dtb

Now use below Query to take the columns from a datatable in an incremental Order.

dtb=dta.DefaultView.ToTable(False,ListA.AsEnumerable.Skip(n*inta).Take(inta).ToArray())

then Increment int n= intn+1 and int K = int K+1

Then use Write Range to Write the DataTable dtb
Give the Sheet Name “Sheet”+intK.ToString

Regards,
Mahesh

2 Likes

Thank you will try this

Datatable Results_of_two_cols = Yourdatatable.Select().CopyToDataTable().DefaultView.ToTable(False, “Date”, “Close”)
this worked perfectly

@ssk

You told to create 50 Seperate tables with 10 columns each right.

Regards,
Mahesh

yes I modified the above code as per my requirement. I got this one from one of the forum.

Thanks,
Siva