How to bind a DataSet with multiple tables directly to a DataGridView

I've got a DataSet containing multiple table, with relationships set between them, and I just want to bind the whole thing to a DataGridView.  I'm not getting any errors, but the data is not showing up in my grid.

I've tried both these variations:

dgmain.DataSource = mdsTable;
dgmain.DataSource = mdsTable.DefaultViewManager;

I've done each of these separately just to verify that there is data in each table, and everything displays just fine:

dgmain.DataSource = mdsTable.Tables[0].DefaultView;
dgmain.DataSource = mdsTable.Tables[1].DefaultView;
dgmain.DataSource = mdsTable.Tables[2].DefaultView;

Anybody know what the correct method is for programatically binding a complex dataset to a DataGridView?
Who is Participating?
anyoneisConnect With a Mentor Commented:
The DataGridview is designed to display one datasource - e.g. one table.

If you want to allow the user to navigate between several tables mapped by parent-child relations, you would use a DataGrid. and make the DataSource point to the DataSet, and the DataMember point to the Parent Table.

If you want to combine all three tables horizontally creating, essentially, a join of the 3, you could do it in SQL, or programatically build a new table with all three's columns and data in the dataset. Then display in the DataGridView

If you want to combine all three tables vertically creating, essentially, a union of the 3, you could do it in SQL, or programatically build a new table with all three's row sets. Then display in the DataGridView.


FrancineTaylorAuthor Commented:

I want a complex (multiple table) dataset to be displayed in the grid, with parent-child relations that you can drill down to.

I tried your first suggestion and something just isn't working.  The code looks like this:

string[] sa = new string[] { "org_placement", "org_role_fulfillment", "request" };

// This is a method which returns a multiple-table dataset, with relationships all set up.  I went in with the debugger
//   and it looks like it is setting up the data and the relationships properly.
mds = my.db.GetMultipleDataSet("organization", sa, "org_id", "10");

if (mds != null)
    dgmain.DataSource = mds;
    dgmain.DataMember = "organization";

What happens is that the primary table (organization) shows up in the grid but none of the child tables seem to be there.  When I set up one of the child tables to be the primary (dgmain.DataMember = "org_placement") or (dgmain.DataSource = mds.Tables[2].DefaultView) I can see that there is data to be displayed.

I just can't get the "multiple" functionality working.
I believe I fully answered the question in my first line: "The DataGridview is designed to display one datasource - e.g. one table."

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

FrancineTaylorAuthor Commented:
That's technically true, but it doesn't solve my problem.  I tried to use a DataGrid to display multiple tables and it isn't working (as outlined in my reply, though in retrospect it looks like I didn't make it clear I had switched to a DataGrid).
Ah! Sorry about that - I didn't understand that you had switched to DataGrid!

There are two possibilities that come to mind:
1) DgMain.AllowNavigation == false

2) The relations have a problem. Can you show me the code you use to create the relations?

FrancineTaylorAuthor Commented:
OK, it works now.  I have no idea how the AllowNavigation flag got set, must be a leftover from a sloppy cut-and-paste.

Thanks, David.  Sorry for not being clear about the control change.
Great! And thanks for the points! I'm Sorry it took so long to find you an answer.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.