Home All Groups Group Topic Archive Search About

Related tables for grids in vs 2005

Author
12 Jan 2006 8:47 PM
Magnus Blomberg
Hello!

I'm quite new to VS 2005, and tries to connect several controls at my form.
I thought this should be simple without writing code, but I can't get it to
work.

I have one dataset with two related tables.
In my form I have two datagridviews. The first has datasource set to the
dataset and datamember set to the first table.
The second has the datasource set to the dataset and datamember set to the
second table.
I've tried more combinations, but really don't understand what to do and how
it works.

What I want: I click a row in the first datagridview, for instance with a
primary key value set to 17.
When I do this I only want to see those rows with foreign key value set to
17 in my second datagrid (and in the second table).
I also want to be able to add rows to the second datagridview, so that the
foreign key value is automatically set to 17.

Shouldn't this be basic functionality, that not requires code???

Please help
/Magnus

Author
12 Jan 2006 11:17 PM
Bart Mermuys
Hi,

Show quote
"Magnus Blomberg" <magnus.blomb***@skanska.se> wrote in message
news:%23Ni6yn7FGHA.3892@TK2MSFTNGP12.phx.gbl...
> Hello!
>
> I'm quite new to VS 2005, and tries to connect several controls at my
> form.
> I thought this should be simple without writing code, but I can't get it
> to
> work.
>
> I have one dataset with two related tables.
> In my form I have two datagridviews. The first has datasource set to the
> dataset and datamember set to the first table.
> The second has the datasource set to the dataset and datamember set to the
> second table.
> I've tried more combinations, but really don't understand what to do and
> how
> it works.

In NET2.0 it's more convenient if you put BindingSource's between them, so
it should look like:

MasterBindingSource
    DataSource=someDataSet
    DataMember="MasterTable"

ChildBindingSource
    DataSource=someDataSet
    DataMember="MasterTable.RelationToChildTable"

MasterDataGridView
   DataSoure = MasterBindingSource

ChildDataGridView
   DataSource = ChildBindingSource

>
> What I want: I click a row in the first datagridview, for instance with a
> primary key value set to 17.
> When I do this I only want to see those rows with foreign key value set to
> 17 in my second datagrid (and in the second table).
> I also want to be able to add rows to the second datagridview, so that the
> foreign key value is automatically set to 17.
>
> Shouldn't this be basic functionality, that not requires code???

Make sure you have a Data Source ( Typed DataSet + TableAdapters ).

Menu->Data->Show Data Sources, create a new Data Source for the two tables
if you haven't already.  Then right click on the DataSet and choose open
'Edit DataSet with designer" and verify there is a relation between the two
DataTable (indicated by a line), if there isn't then right-click on the
empty space and choose add-relation.

Now inside Data Sources window you should see your two tables.  The tables
have grids to the left of them while the fields have TextBox's on the left.
So you can either drag the master table to the Form or some of the master
table fields.
Then you should also see inside Data Sources window that the master table
has a field which also has a grid to the left (child table because of the
relation).  Now you can also drag that child table on the Form or any of
it's field.

That should setup most things, except you still need to add some code to the
save button.

See also:
http://www.msdn.microsoft.com/vbasic/default.aspx?pull=/library/en-us/dnvs05/html/newdtastvs05.asp


HTH,
Greetings


Show quote
>
> Please help
> /Magnus
>
>
Author
12 Jan 2006 11:32 PM
Bart Mermuys
[Snipped]

> In NET2.0 it's more convenient if you put BindingSource's between them, so
> it should look like:
>
> MasterBindingSource
>    DataSource=someDataSet
>    DataMember="MasterTable"
>
> ChildBindingSource
>    DataSource=someDataSet
>    DataMember="MasterTable.RelationToChildTable"

Made a mistake with ChildBindingSource, should be:
ChildBindingSource
    DataSource=MasterBindingSource
    DataMember="RelationToChildTable"

>
> MasterDataGridView
>   DataSoure = MasterBindingSource
>
> ChildDataGridView
>   DataSource = ChildBindingSource
>

Greetings


Show quote
>>
>> What I want: I click a row in the first datagridview, for instance with a
>> primary key value set to 17.
>> When I do this I only want to see those rows with foreign key value set
>> to
>> 17 in my second datagrid (and in the second table).
>> I also want to be able to add rows to the second datagridview, so that
>> the
>> foreign key value is automatically set to 17.
>>
>> Shouldn't this be basic functionality, that not requires code???
>
> Make sure you have a Data Source ( Typed DataSet + TableAdapters ).
>
> Menu->Data->Show Data Sources, create a new Data Source for the two tables
> if you haven't already.  Then right click on the DataSet and choose open
> 'Edit DataSet with designer" and verify there is a relation between the
> two DataTable (indicated by a line), if there isn't then right-click on
> the empty space and choose add-relation.
>
> Now inside Data Sources window you should see your two tables.  The tables
> have grids to the left of them while the fields have TextBox's on the
> left. So you can either drag the master table to the Form or some of the
> master table fields.
> Then you should also see inside Data Sources window that the master table
> has a field which also has a grid to the left (child table because of the
> relation).  Now you can also drag that child table on the Form or any of
> it's field.
>
> That should setup most things, except you still need to add some code to
> the save button.
>
> See also:
> http://www.msdn.microsoft.com/vbasic/default.aspx?pull=/library/en-us/dnvs05/html/newdtastvs05.asp
>
>
> HTH,
> Greetings
>
>
>>
>> Please help
>> /Magnus
>>
>>
>
>
Author
13 Jan 2006 8:17 AM
Cor Ligthert [MVP]
Magnus,

> Shouldn't this be basic functionality, that not requires code???

Net is not the same as Office MS Access.

It gives you a lot rapid design tools to do the most standard things.

However as soon as we seen things as updating than we are allowed (have to
do) it ourselves.

I hope this helps,

Cor

AddThis Social Bookmark Button