Solved

C# DataRelation into a Datatable

Posted on 2014-07-18
5
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 63

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 63

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 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