I have a DataTable called "DTHead" which has the following records,
 MIVID      Quantity         Value
------     ----------       --------
   1           10             3000
   1           20             3500
   1           15             2000
   2           20             3000
   2           50             7500
   3           25             2000
Here, I need to split the above DataTable into three tables based on the MIVID such as follows;
DTChild1:
  MIVID           Quantity        Value
 -------         ----------     ---------
   1                10             3000
   1                20             3500
   1                15             2000
DTChild2:
  MIVID           Quantity        Value
 -------         ----------     ---------
   2                20             3000
   2                50             7500
DTChild3:
  MIVID           Quantity        Value
 -------         ----------     ---------
    3               25             2000
Suppose, if the Header DataTable contains 4 different MIVID means, then 4 Child DataTable should be created based on the MIVID. How to do this?
Use LINQ to DataTable to group the first column by GroupBy, and use method CopyToDataTable to copy list of rows to DataTable
 List<DataTable> result = DTHead.AsEnumerable()
            .GroupBy(row => row.Field<int>("MIVID"))
            .Select(g => g.CopyToDataTable())
            .ToList();
Then you can get the result as a list of DataTables as you expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With