I want to split single DataTable into multiple DataTable
s.
Table1 contains clientid, clientname
(1,client1)(1,client2)(1,client3)(2,client4)(2,client5)
I want to split this table into
table2 = (1,client1)(1,client2)(1,client3)
and
table3 will have (2,client4)(2,client5).
The same clientid DataRow
s will moved to a separate DataTable
. How can I do that?
I tried, but it's not working. I want to do this without linq in c#. How to Split datatable into multiple datatable using particular id without using linq in c#?
foreach (DataRow row in dsBindSubCategory.Tables[0].Rows)
{
DataRow newRow = newDt.NewRow();
newRow.ItemArray = row.ItemArray;
newDt.Rows.Add(newRow);
i++;
if (Convert.ToInt32(dsBindSubCategory.Tables[0].Rows[i]["ClientId"]) != Convert.ToInt32(dsBindSubCategory.Tables[0].Rows[i - 1]["ClientId"]))
{
newDs.Tables.Add(newDt);
j++;
newDt = dsBindSubCategory.Tables[0].Clone();
newDt.TableName = "Table_" + j;
newDt.Clear();
i = 0;
}
}
return newDs;
The following code will create two data tables from one data table. Note that I'm creating the original table in code as well, since I don't have access to your data source.
So, obviously this is creating just two tables. Since your comment says you want multiple new data tables for each unique id, you'd want to use the following code:
each time you iterate through the rows in the source table and find a new clientid. You'd probably want some sort of dictionary of ids and new result tables to keep track of things. If you need more details I can try to write up a more full example, but the concept is the same.