473,388 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

DataTable question

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.
Nov 19 '05 #1
1 950
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/
"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:Pv********************@rogers.com...
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.

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stefan Turalski \(stic\) | last post by:
Hi, I'm wondering if there is a way to send a method parametrs by ref when DataTabel is a type of this value ? I done some sort of select over DataTable columns, just by removing them froma...
1
by: Mike | last post by:
I have an ASP.NET/VB app that updates values in a DataTable over the course of about 3 different pages. On the way out of the first of these pages, I explicitly build the DataTable from values in...
2
by: JohnR | last post by:
Sorry if this is has an obvious answer, but I can't find it... I am using a datatable.select to obtain the rows in a datatable that meet certain criteria. My question is this: for each of the...
6
by: Danny Ni | last post by:
Hi, If I want to programatically add rows to a DataTable, do I call AcceptChanges per row? Or do I call AcceptChanges after all rows added? TIA
1
by: kempshall | last post by:
Hi, I have a question about the .NET DataTable.ImportRow method. If I import a DataRow into an empty DataTable, will the empty DataTable pick up the schema of the DataRow I'm trying to put into...
4
by: George | last post by:
Got a question about the side effect of DataAdapter.Update() and DataTable.GetChanges(). Say I set up a DataTable and a DataAdapter in a class. Delete (Not remove) a row in the data table and...
1
by: VMI | last post by:
When my webpage loads the first time, I fill my datatable with records from the DB and display them in my gridview. Once it loads, the user clicks on a button and add records to the datatable,...
1
by: Maxwell2006 | last post by:
Hi, I am working with strongly typed datatables. What is the most efficient way to build a new DataTAble based on the result of DataTable.Select? At this point I use a foreach loop to do the...
5
by: jehugaleahsa | last post by:
Hello: What is the point of using a DataTable in ASP .NET? We are unsure how you can use them without 1) rebuilding them every postback, or 2) taking up precious memory. We are not sure how to...
9
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got a routine that builds a table using different queries, different SQL Tables, and adding custom fields. It takes a while to run (20 - 45 seconds) so I wrote a thread to handle the table...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.