I have a DataTable which has many columns and only one row:
...
DataSet myDataSet = new DataSet();
da.Fill(myDataSet);
myDataTable = new DataTable();
myDataTable = myDataSet.Tables[0];
...
How can I split the DataSet/DataTable to have equal amount of columns (if it is an odd number, split the columns so the first DataTable
has the extra column).
Scenario #1:
Main DataTable
:
col1 col2 col3 col4 col5 col6 col7 col8
9 0 9 5 7 4 9 3
DataTable1
:
col1 col3 col3 col4
9 0 9 5
DataTable2
:
col5 col6 col7 col8
7 4 9 3
Scenario #2:
Main DataTable
:
col1 col2 col3 col4 col5 col6 col7
9 0 9 5 7 4 9
DataTable1
:
col1 col3 col3 col4
9 0 9 5
DataTable2
:
col5 col6 col7
7 4 9
You can copy the whole data table and then remove the columns you don't want.
So for your first example, following code will return first four columns in datatable1 and remaining columns in datatable 2.
You can modify your code as per your number of columns
You can as well use the overloaded version of
DataView.ToTable(String, Boolean, String[])
to achieve this passing the required column names like below