Solved

C# DataRelation into a Datatable

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

690 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