Groups | Blog | Home
all groups > asp.net > february 2005 >

asp.net : DataTable question


Maziar Aflatoun
2/4/2005 11:28:58 PM
Hi,

I have two datatables,

DataTable1
dtCurProducts.Columns.Add(new DataColumn("Id",
Type.GetType("System.String")));
dtCurProducts.Columns.Add(new DataColumn("ProductId",
Type.GetType("System.String")));
dtCurProducts.Columns.Add(new DataColumn("Quantity",
Type.GetType("System.String")));
dtCurProducts.Columns.Add(new DataColumn("FinalPrice",
Type.GetType("System.String")));


DataTable2
dtProducts.Columns.Add(new DataColumn("Id", Type.GetType("System.String")));
dtProducts.Columns.Add(new DataColumn("Name",
Type.GetType("System.String")));
dtProducts.Columns.Add(new DataColumn("ProductCode",
Type.GetType("System.String")));
dtProducts.Columns.Add(new DataColumn("DefaultPrice",
Type.GetType("System.String")));

DataTable2 contains a list of all of my products. DataTable1 contains the
Quantity and the FinalPrice. Now I like to create another DataTable or
expand DataTable1 as I'm creating it to include 3 additional fields taken
from DataTable2 so that my end result would be a DataTable containing
ProductId, Quantity, Name, ProductCode, DefaultPrice and FinalPrice
(Relationship between the 2 tables defined by
DataTable1.ProductId=DataTable2.Id

Can someone please tell me how I can accomplish this? Maybe as I'm creating
the contents of DataTable1 (Given I have DataTable2)

Thank you
Maziar A.

Karl Seguin
2/5/2005 8:28:34 AM
Maziar:
Why not create a DataRelation between the two? That's what its there for.
If your two DataTables are inside the same DataSet, you can easily do this
via:

ds.Relations.Add(new DataRelation("RelationName",
ds.Tables[0].Columns["ProductId"], ds.Tables[1].Columns["Id"]));

this creates a relationship between hte two...

Now ,given a row from DataTable1, you can get the children row(s) of
DataTable2 via row.GetChildRows("relationName");

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


[quoted text, click to view]

AddThis Social Bookmark Button