How to Split datatable into multiple datatable using particular id without using linq in c#?

1.6k views Asked by At

I want to split single DataTable into multiple DataTables.

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 DataRows 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;
2

There are 2 answers

2
AngularRat On

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.

    DataTable table = new DataTable();

    DataColumn col1 = new DataColumn("clientid");
    DataColumn col2 = new DataColumn("clientname");

    col1.DataType = System.Type.GetType("System.Int32");
    col2.DataType = System.Type.GetType("System.String");

    table.Columns.Add(col1);
    table.Columns.Add(col2);

    DataRow r = table.NewRow();
    r[col1] = 1;
    r[col2] = "client 1";
    table.Rows.Add(r);
    r = table.NewRow();
    r[col1] = 1;
    r[col2] = "client 2";
    table.Rows.Add(r);
    r = table.NewRow();
    r[col1] = 2;
    r[col2] = "client 3";
    table.Rows.Add(r);

    // Create two new data tables
    DataTable dt1 = new DataTable("t1");
    DataTable dt2 = new DataTable("t2");
    // Make the columns of the new tables match the existing table columns
    foreach(DataColumn dc in table.Columns)
    {
        dt1.Columns.Add(new DataColumn(dc.ColumnName, dc.DataType));
        dt2.Columns.Add(new DataColumn(dc.ColumnName, dc.DataType));
    }

    foreach (DataRow row in table.Rows)
    {
        int id = Convert.ToInt32(row["clientid"]);
        if (id == 1)
        {
            DataRow dr = dt1.NewRow();
            dr.SetField("clientid", row["clientid"]);
            dr.SetField("clientname", row["clientname"]);
            dt1.Rows.Add(dr);
        }
        else
        {
            DataRow dr = dt2.NewRow();
            dr.SetField("clientid", row["clientid"]);
            dr.SetField("clientname", row["clientname"]);
            dt2.Rows.Add(dr);
        }
    }
}

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:

    DataTable newTable= new DataTable();
    // Make the columns of the new tables match the existing table columns
    foreach(DataColumn dc in table.Columns)
    {
        newTable.Columns.Add(new DataColumn(dc.ColumnName, dc.DataType));
        newTable.Columns.Add(new DataColumn(dc.ColumnName, dc.DataType));
    }

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.

2
Graffito On

You may create DataViews and copy them to dataTables:

DataView DataView1 = new DataView(table,"ClientId=1","ClientId ASC", DataViewRowState.CurrentRows);
DataTable dt1 = DataView1.Table.Clone();