Thryn
Thryn

Reputation: 455

Merging two Datatables with different column names

I have two Datatables with differents columns name because they a filled from two different MySQL tables

I wanted to merge both of them so I can display both in DataGridView1. I did

DataTable2.Columns(3).ColumnName = "Genre" DataTable2.Columns(4).ColumnName = "Creator"

And then DataTable1.Merge(DataTable2, false, MissingSchemaAction.Add) BindingSource1.DataSource = DataTable1 DataGridView1.Source = BindingSource1

But on display, it only shows the data from DataTable2

Upvotes: 0

Views: 885

Answers (1)

Steve
Steve

Reputation: 216353

Probably the problem is caused by the fact that Merge uses the PrimaryKey of the table to find an existing record to update and if it can't find it then add the new record. If this is the case then you should disable the PrimaryKey info retrieved when you have filled the table through the data adapter.

dataTable1.PrimaryKey = Nothing
dataTable2.PrimaryKey = Nothing
dataTable1.Merge(dataTable2, false, MissingSchemaAction.Add)
....

Now Merge cannot find the matches and thus every record in dataTabl2 is added to the dataTable1. However I should warn you to keep an eye on the performances and correctness of other operations on this dataTable1.
Now there is no PrimaryKey set and this could be a source of problems in updating and deleting a row (if you have these operations of course)

Upvotes: 1

Related Questions