Amarnath Balasubramanian
Amarnath Balasubramanian

Reputation: 9460

Split a DataTable into 2 or more DataTables based on division of Column value

The Datatable I'm having

 

  EmpID    |    EmpName   |  Values |   Rowindex
 _______        _______      _______    _______

   1             a            10         0
   2             b            10         1
   3             c            10         2
   4             d            10         3
   5             e            10         4
   6             f            10         5
   7             g            10         6
   8             h            10         7
   9             i            10         8

The Datatables i need from the above is

Datatable 1 :

 
  EmpID    |    EmpName   |  Values |   Rowindex
 _______        _______      _______    _______

   1             a            10         0
   2             b            10         1
   3             c            10         2
   4             d            10         3
   5             e            10         4

Datatable 2


  EmpID    |    EmpName   |  Values |   Rowindex
 _______        _______      _______    _______
   6             f            10         0
   7             g            10         1
   8             h            10         2
   9             i            10         3

Condition to split:

P.s: I know to split based on column values. Dont know to split after calculations.

Coding to split based on Columnvalues:

RequestDS = MYDataset

            Dim dsTablesById As New DataSet()
            For Each row As DataRow In RequestDS.Tables(0).Rows
                Dim ID As String = row("ID").ToString()
                If Not dsTablesById.Tables.Contains(ID) Then
                    dsTablesById.Tables.Add(ID)
                    For Each col As DataColumn In RequestDS.Tables(0).Columns
                        dsTablesById.Tables(ID).Columns.Add(col.ColumnName, col.DataType)
                    Next
                End If
                dsTablesById.Tables(ID).Rows.Add(row.ItemArray)
                dsTablesById.Tables(ID).AcceptChanges()
                ViewState("TableDataset") = dsTablesById
            Next

Upvotes: 0

Views: 1288

Answers (1)

David
David

Reputation: 302

Dim ID As String = row("ID").ToString() 

should be

Dim ID As String = (row("ID") mod 5).ToString()

and do the following before you add the row :

row.ItemArray(3) = dsTablesById.Tables(ID).Rows.Count

Upvotes: 1

Related Questions