Why can't I create a DataGridView whose datasource has > 1 table

G

Guest

Hi,

I am working inside the DataSet designer. I have 2 tables related by one
relation and FK. Both tables have been configured to be updatable
(insert,update,delete methods generated).

All this is nice. Yet, I need to display a join of these tables selecting
only certain columns on each table.
I expected the new binding technology would allow me to do this and bind it
to a DataGridView AND make the rows in that grid updatable (after all each
column belongs to only one table and we know how to update each table).

But how?
 
M

Marina Levit [MVP]

No, it doesn't quite work this way.

The built in wizard doesn't know how to do what you are asking it to. I
would assume it was too complicated to build the logic to figure this out,
have multiple statements for each operation, etc. In fact, it can be very
hard to determine what the WHERE clause for the update should be - after
all, each table has its own primary key, and the wizard has to go and figure
all this out.

You can easily display data that is the result of a join. However, you are
on your own in terms of updating it.

YOU might know how to correctly update each row. That doesn't mean the
wizard can, in a generic manner easily figure it out.
 
G

Guest

Hi Juan,

The best way to get around this would probably be to do your join inside a
select stored proc that returns your joined results and then create
corresponding insert, update and delete stored procs that take the correct
parameters. Then create a table in your strongly typed dataset based on the
select stored proc (either by draging from the Server Explorer or adding it
manually). This way to the dataset and to the Win Forms databinding
mechanism, it really does just look like a single table.
 
C

cjard

Juan said:
Hi,

I am working inside the DataSet designer. I have 2 tables related by one
relation and FK. Both tables have been configured to be updatable
(insert,update,delete methods generated).

All this is nice. Yet, I need to display a join of these tables selecting
only certain columns on each table.
I expected the new binding technology would allow me to do this and bind it
to a DataGridView AND make the rows in that grid updatable (after all each
column belongs to only one table and we know how to update each table).

But how?

I've not seen a DataGridView used relationally in the same way that the
legacy DataGrid is, but now you have your tables and relation in place
you drop the following on a form:

2 DataGridViews - called master/child
2 BindingSources - called master/child
2 tableadapters - called master/child
1 dataset - tables called master/child

master DGV is bound to master BS
master BS is bound to datasource=dataset datamember=master
child DGV is bound to child BS
child BS is bound to datasource=master BS datamember =
FK_master_child_datarelation

the tables are loaded with related data:

masterTA.FillByWhateverClause(dataset.master, clause, clause...)
childTA.ClearBeforeFill=false;
for each(masterDataRow ro in master.Rows){
childTA.FillByMasterID(dataset.child, ro.masterID)
}


now you have 2 tables with e.g 10 master rows, 100 child rows. when you
select a row in the master DGV, the rows in the child DGV adjust to
show child related records

all are updatable
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top