Thursday, June 12, 2008

Merging two dataTables

In this method I create a TEMP Table because I have two tables of information that I need to merge together (some names and their matching data) ---so I create a new table and clone it -- Clone copies the data structure of one table to another.
Dim tempTable As DataTable

'I clone the normal table that we need so I can insert the proper users later.
tempTable = dataset1.Tables(1).Clone

THEN we start going through our two tables and merging the users with their data-- INTO our temp table.
For iRowCount = 0 To dsReports.Tables(0).Rows.Count - 1

'Create the teachername for the rows
If Not dataset1.Tables(0).Rows(iRowCount)("fname") Is DBNull.Value Then teachername = dataset1.Tables(0).Rows(iRowCount)("fname")

If Not dataset1.Tables(0).Rows(iRowCount)("lname") Is DBNull.Value Then teachername = teachername + " " + dataset1.Tables(0).Rows(iRowCount)("lname")

'Now iterate through the other table and match the names via the matching ID
For iRowCount2 = 0 To dataset1.Tables(1).Rows.Count - 1

If dataset1.Tables(0).Rows(iRowCount)("sessid") = dataset1.Tables(1).Rows(iRowCount2)("session") Then

tempTable.ImportRow(dataset1.Tables(1).Rows(iRowCount2))
tempTable.Rows(tempTable.Rows.Count - 1)("teachername") = teachername

End If

Next

Next

FINALLY, we remove the two seperate tables that are no longer needed and we insert our finished temp table that will work with the rest of the code.

dataset1.Tables.Remove(dataset1.Tables(0))

dataset1.Tables.Remove(dataset1.Tables(1))

dataset1.Tables.Add(tempTable)

No comments: