Solved

C# DataRelation into a Datatable

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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