tags:

views:

452

answers:

3

I have two datatables. One is a superset of another. Both have one column StageID common. I need to merge these two tables together so that the subset datatables data gets stored in the correct row of superset table.

This is what i did to join the data after retrieving the superset data in JoinTable and subset data in DTMaterialProperties table

foreach (DataColumn dcMaterialProp in dtMaterialProperties.Columns) 
{
   if (dcMaterialProp.ColumnName != "StageId" &&  
      JoinDataTable.Columns.Contains(dcMaterialProp.ColumnName))
   {
      JoinDataTable.Rows[rowCount][dcMaterialProp.ColumnName] = 
         dtMaterialProperties.Rows[0][dcMaterialProp.ColumnName];
   }
}

But this was not efficient as it takes a lot of time in looping through. Please Help me in finding a better way to do this.

A: 

If you can use LINQ, an easy way would be to use the Join extension method.

See Join Method-Based Query Syntax Examples (LINQ to DataSet)

Pierre-Alain Vigeant
A: 

If you don't have access to LINQ to DataSet

...you can check out this link from MSDN on how to implement a DataSet JOIN Helper Class.

HOW TO: Implement a DataSet JOIN helper class is Visual C# .NET

Justin Niessner
A: 

What you are really looking for seems to be a relationship. If you add both datatables to the same dataset, you can define the relationship. See here: http://vb-helper.com/howto%5Fnet%5Fdatagrid%5Frelations.html

Then, all you have to do is parentRow.GetChildRows() to get the associated child rows.

Erich