mike44 mike44 - 1 year ago 79
C# Question

Splitting the datatable into multiple datatables

These are the contents of my datatable


column_1 column_2 column_3
-------- -------- --------
sec_1 Test1 2
sec_1 Result1 5
sec_1 Unit1 2
sec_2 Test2 2
sec_2 Result2 2
sec_2 Unit2 5
sec_3 Test3 2
sec_3 Result3 2
sec_3 Unit3 2

I need to split it into multiple datatables on the basis of contents in column

So in this case I'm supposed to get 3 tables (one having all rows with
, other with
& another with

I tried this:

var dtArray = dtAllData.AsEnumerable()
.GroupBy(row => new
column_1 = (string)row["column_1"]
DataTable[] array = new DataTable[dtArray.Count()];

How can I get tables in

Kaf Kaf
Answer Source

Using CopyToDataTable() method to get the data into a new DataTable.

var Result =  from x in dtAllData.AsEnumerable()
              where x.Field<string>("column_1") == "sec_1"
              select x;
DataTable t1 = Result.CopyToDataTable();

Another way is using LoadDataRow() method. Here is an example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download