Solved

C# DataRelation into a Datatable

Posted on 2014-07-18
5
223 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now