Join tables in vb.NET

377 Views Asked by At

There's a lot of posts (on this and other sites) about this matter, but every solution I read involves SQL queries and I'm not looking for such a solution.

I have three DataTables (vb.NET) and I want to join them by the first column (which contains a TimeStamp).

For instance, if I have three DataTables called TAB01, TAB02 and TAB03 filled like the next ones:

          TAB01                          TAB02                          TAB03
TimeStamp             Value1 | TimeStamp             Value2 | TimeStamp             Value3
2019/10/09 09:00:00   0.2334 | 2019/10/09 09:00:00   1.1212 | 2019/10/09 09:00:00   2.1221
2019/10/09 09:01:00   0.1111 | 2019/10/09 09:01:00   4.1211 | 2019/10/09 09:01:00   3.1766
2019/10/09 09:02:00   2.6566 | 2019/10/09 09:02:00   3.2122 | 2019/10/09 09:02:00   0.1322

how can I fill a fourth DataTable (say TAB04) like the next one?

                                         TAB04                        
                    | TimeStamp             Value1  Value2  Value3 |
                    | 2019/10/09 09:00:00   0.2334  1.1212  2.1221 |
                    | 2019/10/09 09:01:00   0.1111  4.1211  3.1766 |
                    | 2019/10/09 09:02:00   2.6566  3.2122  0.1322 |

The problem seems quite simple (my fist attempt was using collections to save the data from all three tables and then matching the TimeStamps to fill the fourth table). However, since I can't guarantee that the TimeStamps from the three tables match with each other, the collections technique looks like the worst option.

Is there another way to approach this question?

UPDATE:

I found this approach here that uses a DataSet to create a DataRelation between to columns of two different tables:

'Create DataSet:
    Dim ds As DataSet = New DataSet 
    'Add tables to DataSet:
        ds.Tables.Add(TAB01)
        ds.Tables.Add(TAB02)
    'Create DataRelation:
        Dim dr As DataRelation = New DataRelation("dr", TAB01.Columns(0), TAB02.Columns(0))
    'Add DataRelation to DataSet:
        ds.Relations.Add(dr)

So far so good (I understand perfectly the syntax of the code above. However, as it is mentioned on the link, the command:

DGV1.DataSource = ds

should fill the DataGridView (named DGV1) automatically as the table TAB04. Something's missing because this code doesn't fill DGV1 with anything. Is this a valid method? If so, what am I missing? Why DGV1's still empty?

0

There are 0 best solutions below