0

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?

Pspl
  • 1,398
  • 12
  • 23
  • is a datagrid an option? cause it's not sql related – OctaCode Oct 09 '19 at 10:03
  • @Devcon, this is for a program to run on a background scenario. It practically doesn't have any user design interface. So I'm not so much interested in inserting a datagrid on the form. Can you tell me how a datagrid is a good approach? – Pspl Oct 09 '19 at 10:09
  • you can use a datagridview without adding it to design: `Dim dgv As New DataGridView` – OctaCode Oct 09 '19 at 10:12
  • Ok! You're right. But how I join the three tables on a datagrid? – Pspl Oct 09 '19 at 10:14
  • Does [LINQ join two DataTables](https://stackoverflow.com/questions/20760681/linq-join-two-datatables) help? – Andrew Morton Oct 09 '19 at 11:10
  • Not really! I don't know how to work with it... – Pspl Oct 09 '19 at 12:15
  • 1
    https://forums.asp.net/t/1232129.aspx?Combining+2+datatables+into+1+datatable+based+on+a+common+field+ create a dataset, add the tables, add the dataset relation, add the column to the second table. Try it – OctaCode Oct 09 '19 at 12:29
  • Thanks for the link @Devcon. I think I got it! – Pspl Oct 09 '19 at 12:40
  • If data always looks like that, you could simply add rows in for x to row.count loop where datatable4.rows.add(dt1(col,row).value,dt1..,dt2..,dt3..) – CruleD Oct 09 '19 at 13:55

0 Answers0