Solved

C# DataRelation into a Datatable

Posted on 2014-07-18
5
229 Views
Last Modified: 2014-07-24
I have a parent table that has columns A, B, C, D, E, F

I have a child table that has columns B, C, G, H, I J, K

Both reside in a dataset ds

I have created a datarelation using:

DataRelation newRelation = new DataRelation("Rcpts", new DataColumn[] 
            {
                ds.Tables["parent "].Columns["B"],ds.Tables["parent "].Columns["C"] 
            },
               new DataColumn[] 
            { 
                ds.Tables["child"].Columns["B"],ds.Tables["child"].Columns["C"]
            });

Open in new window


There is a one to many relationship between parent and child.

I want to get the contents of both tables into a single table. I'm stumped as to how I would do this. Do I have to go row by row through the child table calling the datarelation for the corresponding parent data? Or, is there a simpler way to do this?
0
Comment
Question by:jczander
  • 3
  • 2
5 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40205343
What type of application are you working on? Windows Forms, WPF, Web.
When you state, "I want to get the contents of both tables into a single table" do you mean you want to display the rows from both tables on the user interface Master / Detail records in a DataGrid type control?
0
 

Author Comment

by:jczander
ID: 40205706
Windows Forms. And yes, I'll be displaying the contents of the joined datatable in a datagridview as part of it. That means no master/detail layout. The parent and child data on one line.
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 total points
ID: 40205788
Hi jczander;

The DataGridView and the DataGrid controls do not support that type of functionality. The relationship you have constructed will allow you to select one line in one DataGridView and have all the related records show up in a second DataGridView or if you use a DataGrid then each line in the main table will have a + icon then when clicked on will open all the child record right underneath it in the same DataGrid. But they do not support showing the parent record and all child record on one line. If you want that type of functionality you will need to construct a new DataTable object with all columns from both tables and then iterate through all row from both tables and add the individual columns into a new row of the new table.
0
 

Accepted Solution

by:
jczander earned 0 total points
ID: 40206838
What I did was create a new table with all of the child columns and the few parent columns I needed. I then went through the child table and added each row to the new join table and long with the parent columns. The result is a single table with the joined data.

foreach (DataRow dr in ds.Tables["B"].Rows)
            {
                DataRow parent = dr.GetParentRow("Rcpts");
                DataRow current = jt.NewRow();

                for (int i = 0; i < ds.Tables["B"].Columns.Count; i++)
                {
                    current[i] = dr[i];
                }
                current["E"] = parent["E"];
                current["F"] = parent["F"];

                jt.Rows.Add(current);
            }

Open in new window

0
 

Author Closing Comment

by:jczander
ID: 40216299
My solution actually solved the problem, but the expert reply at least got me thinking.,
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# 2015 Downloading text file and preserving the line breaks 5 36
Where can I ask about my ASP.NET MVC project? 6 35
Video Player 11 23
C# Desktop Application 3 30
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question