These two datatables were queried from DIFFERENT database servers. SQL Join is not an option.
They are filled separately and I need them merged. They're actually quite large, column-wise, but for ease of my question, I simplified them.
Table A (dtA): Definitely some duplicate order numbers...
+-------+-----+
| Order | Lne |
+-------+-----+
| 101 | 1 |
| 123 | 1 |
| 123 | 2 |
| 215 | 1 |
| 333 | 1 |
| 333 | 2 |
| 333 | 3 |
| 333 | 4 |
| 405 | 1 |
| 405 | 2 |
| 405 | 3 |
| 500 | 1 |
+-------+-----+
Table B (dtB): Unique order numbers, but extra data I need
+-------+------------+------------+
| Order | ReqDate | DelivDate |
+-------+------------+------------+
| 215 | 03/15/2014 | 03/15/2014 |
| 333 | 04/02/2014 | 04/15/2014 |
| 405 | | 02/01/2014 |
+-------+------------+------------+
Combined Table (dtCombined): Hopeful goal
+-------+-----+------------+------------+
| Order | Typ | ReqDate | DelivDate |
+-------+-----+------------+------------+
| 101 | 1 | | |
| 101 | 2 | | |
| 101 | 3 | | |
| 215 | 1 | 03/15/2014 | 03/15/2014 |
| 333 | 1 | 04/02/2014 | 04/15/2014 |
| 333 | 2 | 04/02/2014 | 04/15/2014 |
| 333 | 3 | 04/02/2014 | 04/15/2014 |
| 333 | 4 | 04/02/2014 | 04/15/2014 |
| 405 | 1 | | 02/01/2014 |
| 405 | 2 | | 02/01/2014 |
| 405 | 3 | | 02/01/2014 |
| 500 | 1 | | |
+-------+-----+------------+------------+
After combining, I will be displaying in a DataGrid.
Code attempt...
Current attempt at merging the tables:
Dim colsA() As String = {"HeadOrderNo", "Line"}
Dim colsB() As String = {"OrderNo", "RequestDate", "DeliveryDate"}
'Add the columns from the first datatable (dtA)
For nCount1 As Integer = 0 To dtA.Columns.Count - 1
dtCombined.Columns.Add(New DataColumn(dtA.Columns(nCount1).ColumnName, dtA.Columns(nCount1).DataType))
Next
'Add the columns from the second datatable (dtB)
For nCount2 As Integer = 0 To dtB.Columns.Count - 1
dtCombined.Columns.Add(New DataColumn(dtB.Columns(nCount2).ColumnName, dtB.Columns(nCount2).DataType))
Next
'Dim bFirstRunDone As Boolean = False
Dim sColumnName As String
Dim drNewRow As DataRow
Dim nOuter As Integer = 0
'Go through each row in dtA and copy it to dtCombined
For Each drRowETI As DataRow In dtA.Rows
drNewRow = dtCombined.NewRow()
'Copy all column data from to drRow
For Each sColumnName In colsA
drNewRow(sColumnName) = drRowETI(sColumnName)
Next
'Go through each row in dtB and copy it to drNewRow
For Each drRowLion As DataRow In dtB.Rows
'Copy all column data from to drRow -- not working right
For Each sColumnName In colsB
'If dtA.Columns(nOuter).ColumnName = dtB.Columns(sColumnName).ColumnName Then
drNewRow(sColumnName) = drRowLion(sColumnName)
'End If
Next
Next
'If dtA.Columns.Item(sColumnName).ToString() = dtB.Columns.Item(nCurrentRowNumETI).ToString() Then
'End If
dtCombined.Rows.Add(drNewRow)
nOuter = nOuter + 1
Next
It's showing the first two columns correctly, but just repeating either the first or last row from the second table in their respective columns. I'm used to 2D arrays and iterating through them, but these DataRows are confusing for me. I'm not sure how to properly iterate through them to perform this type of task. It really shouldn't be hard I don't think.
I've tried countless ways like this: Joining two DataTables in C# with non matching data but to no avail. I might be implementing it incorrectly, but even the syntax is messing with me (e.g.: "Show New With { }).
Thank you