Parent -Child relationship

A

Andrea Caldarone

Hi all,

In my project (VB.Net 2003) I've plenty tables linked each other via
DataRelation, for example let's analyze this couple:

Parent Table [Clienti]
Child Table [Indirizzi]

theese two tables exist in a SQL Server 2005 Database, and theese are their
definitions:

CREATE TABLE [dbo].[Clienti](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Cognome] [nvarchar](30) COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Clienti_Cognome] DEFAULT (''),
[Nome] [nvarchar](30) COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Clienti_Nome] DEFAULT (''),
[nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Clienti_Email] DEFAULT (''),
[Note] [ntext] COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Clienti_Note] DEFAULT (''),
CONSTRAINT [PK_Clienti] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


CREATE TABLE [dbo].[Indirizzi](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ClientiID] [int] NOT NULL CONSTRAINT [DF_Indirizzi__ClientiID] DEFAULT
(0),
[Indirizzo] [nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Indirizzi_Indirizzo] DEFAULT (''),
[CAP] [nvarchar](6) COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Indirizzi_CAP] DEFAULT (''),
[Città] [nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Indirizzi_Città] DEFAULT (''),
CONSTRAINT [PK_Indirizzi_] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
USE [NENA]
GO
ALTER TABLE [dbo].[Indirizzi] WITH NOCHECK ADD CONSTRAINT
[FK_Indirizzi_Clienti] FOREIGN KEY([ClientiID])
REFERENCES [dbo].[Clienti] ([ID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Indirizzi] CHECK CONSTRAINT [FK_Indirizzi_Clienti]
GO

In my project theese two SQL Tables became two DataTable of one main dataset
with two distinct adapters (aClienti and aIndirizzi) that fills with
..FillSchema and then with .Fill the main dataset. After that I declare the
dataset's relation as:

dsMain.Relations.Add(New DataRelation("Clienti_Indirizzi",
..Tables("Clienti").Columns("ID"),
..Tables("Indirizzi").Columns("ClientiID")))

Then I bind the parent table to a DataGrid and the DefaultView of the child
table to an other datagrid.
When I select one row of the "parent" datagrid, I simply apply a different
..RowFilter to the dsMain.Tables("Indirizzi") table and all works fine.
Here is my problem:

when I create a new record into the "child" DataGrid, I expect that the
[ClientiID] field of the "child" row is AUTOMATICALLY filled the "parent"
value, but it is filled with is default value (i.e. 0)
have I to manually specify .DefaultValue for the "child" table every time I
select a row on the "parent" table?

Thanks all
 
C

Cor Ligthert [MVP]

Andrea,

AFAIK have you always first to set the parent before you can add a child.
This in all situations I know.

Cor

Andrea Caldarone said:
Hi all,

In my project (VB.Net 2003) I've plenty tables linked each other via
DataRelation, for example let's analyze this couple:

Parent Table [Clienti]
Child Table [Indirizzi]

theese two tables exist in a SQL Server 2005 Database, and theese are
their
definitions:

CREATE TABLE [dbo].[Clienti](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Cognome] [nvarchar](30) COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Clienti_Cognome] DEFAULT (''),
[Nome] [nvarchar](30) COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Clienti_Nome] DEFAULT (''),
[nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Clienti_Email] DEFAULT (''),
[Note] [ntext] COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Clienti_Note] DEFAULT (''),
CONSTRAINT [PK_Clienti] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


CREATE TABLE [dbo].[Indirizzi](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ClientiID] [int] NOT NULL CONSTRAINT [DF_Indirizzi__ClientiID] DEFAULT
(0),
[Indirizzo] [nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL
CONSTRAINT
[DF_Indirizzi_Indirizzo] DEFAULT (''),
[CAP] [nvarchar](6) COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Indirizzi_CAP] DEFAULT (''),
[Città] [nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Indirizzi_Città] DEFAULT (''),
CONSTRAINT [PK_Indirizzi_] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
USE [NENA]
GO
ALTER TABLE [dbo].[Indirizzi] WITH NOCHECK ADD CONSTRAINT
[FK_Indirizzi_Clienti] FOREIGN KEY([ClientiID])
REFERENCES [dbo].[Clienti] ([ID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Indirizzi] CHECK CONSTRAINT [FK_Indirizzi_Clienti]
GO

In my project theese two SQL Tables became two DataTable of one main
dataset
with two distinct adapters (aClienti and aIndirizzi) that fills with
.FillSchema and then with .Fill the main dataset. After that I declare the
dataset's relation as:

dsMain.Relations.Add(New DataRelation("Clienti_Indirizzi",
.Tables("Clienti").Columns("ID"),
.Tables("Indirizzi").Columns("ClientiID")))

Then I bind the parent table to a DataGrid and the DefaultView of the
child
table to an other datagrid.
When I select one row of the "parent" datagrid, I simply apply a different
.RowFilter to the dsMain.Tables("Indirizzi") table and all works fine.
Here is my problem:

when I create a new record into the "child" DataGrid, I expect that the
[ClientiID] field of the "child" row is AUTOMATICALLY filled the "parent"
value, but it is filled with is default value (i.e. 0)
have I to manually specify .DefaultValue for the "child" table every time
I
select a row on the "parent" table?

Thanks all
[/QUOTE]
 
R

RobinS

You know, if you used a couple of Binding Sources for those
two grids, this would be a whole lot easier for you. It would
automatically filter the second grid to match the parent,
and you could capture the Format event and check if a row
is being added, and set the default value accordingly.

You can search
microsoft.public.languages.vb.data for my name (RobinS)
with the subject
"Newbie needs help- parent and child datagrid views"
for my post explaining how to do the data bindings if you're
interested. If you can't find it, re-post and I'll re-post the
code.

Robin S.
--------------------------------

Andrea Caldarone said:
Hi all,

In my project (VB.Net 2003) I've plenty tables linked each other via
DataRelation, for example let's analyze this couple:

Parent Table [Clienti]
Child Table [Indirizzi]

theese two tables exist in a SQL Server 2005 Database, and theese are
their
definitions:

CREATE TABLE [dbo].[Clienti](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Cognome] [nvarchar](30) COLLATE Latin1_General_CI_AS NOT NULL
CONSTRAINT
[DF_Clienti_Cognome] DEFAULT (''),
[Nome] [nvarchar](30) COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Clienti_Nome] DEFAULT (''),
[nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL
CONSTRAINT
[DF_Clienti_Email] DEFAULT (''),
[Note] [ntext] COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Clienti_Note] DEFAULT (''),
CONSTRAINT [PK_Clienti] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


CREATE TABLE [dbo].[Indirizzi](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ClientiID] [int] NOT NULL CONSTRAINT [DF_Indirizzi__ClientiID]
DEFAULT
(0),
[Indirizzo] [nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL
CONSTRAINT
[DF_Indirizzi_Indirizzo] DEFAULT (''),
[CAP] [nvarchar](6) COLLATE Latin1_General_CI_AS NOT NULL CONSTRAINT
[DF_Indirizzi_CAP] DEFAULT (''),
[Città] [nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL
CONSTRAINT
[DF_Indirizzi_Città] DEFAULT (''),
CONSTRAINT [PK_Indirizzi_] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
USE [NENA]
GO
ALTER TABLE [dbo].[Indirizzi] WITH NOCHECK ADD CONSTRAINT
[FK_Indirizzi_Clienti] FOREIGN KEY([ClientiID])
REFERENCES [dbo].[Clienti] ([ID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Indirizzi] CHECK CONSTRAINT [FK_Indirizzi_Clienti]
GO

In my project theese two SQL Tables became two DataTable of one main
dataset
with two distinct adapters (aClienti and aIndirizzi) that fills with
.FillSchema and then with .Fill the main dataset. After that I declare
the
dataset's relation as:

dsMain.Relations.Add(New DataRelation("Clienti_Indirizzi",
.Tables("Clienti").Columns("ID"),
.Tables("Indirizzi").Columns("ClientiID")))

Then I bind the parent table to a DataGrid and the DefaultView of the
child
table to an other datagrid.
When I select one row of the "parent" datagrid, I simply apply a
different
.RowFilter to the dsMain.Tables("Indirizzi") table and all works fine.
Here is my problem:

when I create a new record into the "child" DataGrid, I expect that
the
[ClientiID] field of the "child" row is AUTOMATICALLY filled the
"parent"
value, but it is filled with is default value (i.e. 0)
have I to manually specify .DefaultValue for the "child" table every
time I
select a row on the "parent" table?

Thanks all
[/QUOTE]
 
A

Andrea Caldarone

RobinS said:
You know, if you used a couple of Binding Sources for those
two grids, this would be a whole lot easier for you. It would
automatically filter the second grid to match the parent,
and you could capture the Format event and check if a row
is being added, and set the default value accordingly.

You can search
microsoft.public.languages.vb.data for my name (RobinS)
with the subject
"Newbie needs help- parent and child datagrid views"
for my post explaining how to do the data bindings if you're
interested. If you can't find it, re-post and I'll re-post the
code.

Robin S.

Hello Robin,

first of all thanks for the reply. I've found your post and studied a bit
the code written and:

You write
'Set the data source for the binding source to the dataset.
CustomersBindingSource.DataSource = nwData

what is CustomersBindingSource?

You write
'Add the binding for the child; set its binding source to the same
' one as the parent.
Customers_OrdersBindingSource.DataSource = CustomersBindingSource

what is Customers_OrdersBindingSource?
 
R

RobinS

Andrea Caldarone said:
Hello Robin,

first of all thanks for the reply. I've found your post and studied a
bit the code written and:

You write
'Set the data source for the binding source to the dataset.
CustomersBindingSource.DataSource = nwData

what is CustomersBindingSource?

You write
'Add the binding for the child; set its binding source to the same
' one as the parent.
Customers_OrdersBindingSource.DataSource = CustomersBindingSource

what is Customers_OrdersBindingSource?

There is a BindingSource in the toolbox; drag it onto the
component tray or your form. Then click on it, and
chang the name to correspond to your tables (otherwise they
will be BindingSource and BindingSource1, which would work
but not be terribly clear).

I'm using .Net 2.0 and VB2005; I'm not certain if that's
what you're using, but hopefully if you're doing 2003 version,
the Binding Source was available.

Robin S.
 
A

Andrea Caldarone

There is a BindingSource in the toolbox; drag it onto the
component tray or your form. Then click on it, and
chang the name to correspond to your tables (otherwise they
will be BindingSource and BindingSource1, which would work
but not be terribly clear).

I'm using .Net 2.0 and VB2005; I'm not certain if that's
what you're using, but hopefully if you're doing 2003 version,
the Binding Source was available.

Robin S.

There is no entry called "BindingSource" in my ToolBox, I'm Using Framework
1.1 and VB2003.
 
R

RobinS

Andrea Caldarone said:
There is no entry called "BindingSource" in my ToolBox, I'm Using
Framework 1.1 and VB2003.

Oh, Bummer. It's probably not available; it must be a .Net 2.0
feature. In that case, I don't know how to help you. I jumped
directly from VB6 to VB2005. Sorry!

Robin S.
 
R

RobinS

You're right; I didn't realize she was using "old" technology. ;-)

She can use the BindingContext to go from record to record, but
what about pre-filling the child's field that links to the parent?
IIRC, she's adding a child record to a parent, and wants to prefill
the key field with the right info from the parent.

Robin S.
 
B

Bart Mermuys

Hi,

Andrea Caldarone said:
Hi all,

In my project (VB.Net 2003) I've plenty tables linked each other via
DataRelation, for example let's analyze this couple:

Parent Table [Clienti]
Child Table [Indirizzi]

In my project theese two SQL Tables became two DataTable of one main
dataset
with two distinct adapters (aClienti and aIndirizzi) that fills with
.FillSchema and then with .Fill the main dataset. After that I declare the
dataset's relation as:

dsMain.Relations.Add(New DataRelation("Clienti_Indirizzi",
.Tables("Clienti").Columns("ID"),
.Tables("Indirizzi").Columns("ClientiID")))

Then I bind the parent table to a DataGrid and the DefaultView of the
child
table to an other datagrid.
When I select one row of the "parent" datagrid, I simply apply a different
.RowFilter to the dsMain.Tables("Indirizzi") table and all works fine.
Here is my problem:

when I create a new record into the "child" DataGrid, I expect that the
[ClientiID] field of the "child" row is AUTOMATICALLY filled the "parent"
value, but it is filled with is default value (i.e. 0)
have I to manually specify .DefaultValue for the "child" table every time
I
select a row on the "parent" table?

You can bind to a DataRelation, example :

parentDataGrid.DataSource = dsMain;
parentDataGrid.DataMember = "Clienti";

childDataGrid.DataSource = dsMain;
childDataGrid.DataMember = "Clienti.Clienti_Indirizzi";

- OR -

parentDataGrid.DataSource = dsMain.Tables["Clienti"];

childDataGrid.DataSource = dsMain.Tables["Clienti"];
childDataGrid.DataMember = "Clienti_Indirizzi";


That's basicly the same but don't mix them. For relations to work :

- the child DataGrid needs to use the exact same DataSource as the parent
DataGrid,

- the child DataGrid needs to use the same DataMember as the parent DataGrid
with the relation name appended,

- both DataGrid's need the same BindingContext, but that's the default
anyway

This will automatically filter the child DataGrid when you browse the parent
DataGrid, and it will also automatically insert the parent keys into child
records.


HTH,
Greetings
 
R

RobinS

Bart Mermuys said:
Hi,

Andrea Caldarone said:
Hi all,

In my project (VB.Net 2003) I've plenty tables linked each other via
DataRelation, for example let's analyze this couple:

Parent Table [Clienti]
Child Table [Indirizzi]

In my project theese two SQL Tables became two DataTable of one main
dataset
with two distinct adapters (aClienti and aIndirizzi) that fills with
.FillSchema and then with .Fill the main dataset. After that I
declare the
dataset's relation as:

dsMain.Relations.Add(New DataRelation("Clienti_Indirizzi",
.Tables("Clienti").Columns("ID"),
.Tables("Indirizzi").Columns("ClientiID")))

Then I bind the parent table to a DataGrid and the DefaultView of the
child
table to an other datagrid.
When I select one row of the "parent" datagrid, I simply apply a
different
.RowFilter to the dsMain.Tables("Indirizzi") table and all works
fine.
Here is my problem:

when I create a new record into the "child" DataGrid, I expect that
the
[ClientiID] field of the "child" row is AUTOMATICALLY filled the
"parent"
value, but it is filled with is default value (i.e. 0)
have I to manually specify .DefaultValue for the "child" table every
time I
select a row on the "parent" table?

You can bind to a DataRelation, example :

parentDataGrid.DataSource = dsMain;
parentDataGrid.DataMember = "Clienti";

childDataGrid.DataSource = dsMain;
childDataGrid.DataMember = "Clienti.Clienti_Indirizzi";

- OR -

parentDataGrid.DataSource = dsMain.Tables["Clienti"];

childDataGrid.DataSource = dsMain.Tables["Clienti"];
childDataGrid.DataMember = "Clienti_Indirizzi";


That's basicly the same but don't mix them. For relations to work :

- the child DataGrid needs to use the exact same DataSource as the
parent DataGrid,

- the child DataGrid needs to use the same DataMember as the parent
DataGrid with the relation name appended,

- both DataGrid's need the same BindingContext, but that's the default
anyway

This will automatically filter the child DataGrid when you browse the
parent DataGrid, and it will also automatically insert the parent keys
into child records.


HTH,
Greetings

Thanks all

Won't that work in VB2005/.Net2.0 also? It's basically what I
recommended, but without the BindingSource? Just asking because
it's always good to have more information.

Thanks,
Robin S.
 
B

Bart Mermuys

Hi,

RobinS said:
Bart Mermuys said:
Hi,

Andrea Caldarone said:
Hi all,
[snipped]

Thanks all

Won't that work in VB2005/.Net2.0 also? It's basically what I
recommended, but without the BindingSource? Just asking because
it's always good to have more information.

Yes, that will work in NET2.0 too.

But there are some differences at how it works (internally) ...

* Without BindingSource ( NET1.1 or NET2.0 )
[parent] CurrencyManager - DataView
[child] RelatedCurrencyManager - RelatedDataView

Both CurrencyManager are owned (must) by the same BindingContext (by default
the Form's one). Here a RelatedCurrencyManager detects the change on the
parent CurrencyManager and will query for a new RelatedDataView.

* With BindingSource (NET2.0 only):
[parent] BindingSource - CurrencyManager - DataView
[child] BindingSource - CurrencyManager - RelatedDataView

Both CurrencyManagers are owned by their BindingSource. Here the child
BindingSource will detect the change on the parent BindingSource and will
get the new RelatedDataView and present that to its CurrencyManager so that
is also appears filtered.

The BindingSource is a clever addition because it doesn't break backwards
compatibility, for a huge part it simple relies on it.

The BindingSource :
- is a ICurrencyManagerProvider, it owns a CurrencyManager
- is a IBindingList, it wraps the DataSource and expose itself as the list
to the CurrencyManager it owns
- aggregates some usefull CurrencyManager methods/properties (eg. Position)

Simple Bindings and List Controls will always directly use the
CurrencyManager never the BindingSource (which acts as a front-end), this to
keep backwards compatibility.

HTH,
Greetings
 
C

Cor Ligthert [MVP]

Robin,

Binding with relations has forever been a problem in 1.1. It is not for
nothing that it changed.

Cor

RobinS said:
Bart Mermuys said:
Hi,

Andrea Caldarone said:
Hi all,

In my project (VB.Net 2003) I've plenty tables linked each other via
DataRelation, for example let's analyze this couple:

Parent Table [Clienti]
Child Table [Indirizzi]

In my project theese two SQL Tables became two DataTable of one main
dataset
with two distinct adapters (aClienti and aIndirizzi) that fills with
.FillSchema and then with .Fill the main dataset. After that I declare
the
dataset's relation as:

dsMain.Relations.Add(New DataRelation("Clienti_Indirizzi",
.Tables("Clienti").Columns("ID"),
.Tables("Indirizzi").Columns("ClientiID")))

Then I bind the parent table to a DataGrid and the DefaultView of the
child
table to an other datagrid.
When I select one row of the "parent" datagrid, I simply apply a
different
.RowFilter to the dsMain.Tables("Indirizzi") table and all works fine.
Here is my problem:

when I create a new record into the "child" DataGrid, I expect that the
[ClientiID] field of the "child" row is AUTOMATICALLY filled the
"parent"
value, but it is filled with is default value (i.e. 0)
have I to manually specify .DefaultValue for the "child" table every
time I
select a row on the "parent" table?

You can bind to a DataRelation, example :

parentDataGrid.DataSource = dsMain;
parentDataGrid.DataMember = "Clienti";

childDataGrid.DataSource = dsMain;
childDataGrid.DataMember = "Clienti.Clienti_Indirizzi";

- OR -

parentDataGrid.DataSource = dsMain.Tables["Clienti"];

childDataGrid.DataSource = dsMain.Tables["Clienti"];
childDataGrid.DataMember = "Clienti_Indirizzi";


That's basicly the same but don't mix them. For relations to work :

- the child DataGrid needs to use the exact same DataSource as the parent
DataGrid,

- the child DataGrid needs to use the same DataMember as the parent
DataGrid with the relation name appended,

- both DataGrid's need the same BindingContext, but that's the default
anyway

This will automatically filter the child DataGrid when you browse the
parent DataGrid, and it will also automatically insert the parent keys
into child records.


HTH,
Greetings

Thanks all

Won't that work in VB2005/.Net2.0 also? It's basically what I
recommended, but without the BindingSource? Just asking because
it's always good to have more information.

Thanks,
Robin S.
 
R

RobinS

Bart Mermuys said:
Hi,

RobinS said:
Bart Mermuys said:
Hi,

Hi all,
[snipped]

Thanks all

Won't that work in VB2005/.Net2.0 also? It's basically what I
recommended, but without the BindingSource? Just asking because
it's always good to have more information.

Yes, that will work in NET2.0 too.

But there are some differences at how it works (internally) ...

* Without BindingSource ( NET1.1 or NET2.0 )
[parent] CurrencyManager - DataView
[child] RelatedCurrencyManager - RelatedDataView

Both CurrencyManager are owned (must) by the same BindingContext (by
default the Form's one). Here a RelatedCurrencyManager detects the
change on the parent CurrencyManager and will query for a new
RelatedDataView.

* With BindingSource (NET2.0 only):
[parent] BindingSource - CurrencyManager - DataView
[child] BindingSource - CurrencyManager - RelatedDataView

Both CurrencyManagers are owned by their BindingSource. Here the
child BindingSource will detect the change on the parent BindingSource
and will get the new RelatedDataView and present that to its
CurrencyManager so that is also appears filtered.

The BindingSource is a clever addition because it doesn't break
backwards compatibility, for a huge part it simple relies on it.

The BindingSource :
- is a ICurrencyManagerProvider, it owns a CurrencyManager
- is a IBindingList, it wraps the DataSource and expose itself as the
list to the CurrencyManager it owns
- aggregates some usefull CurrencyManager methods/properties (eg.
Position)

Simple Bindings and List Controls will always directly use the
CurrencyManager never the BindingSource (which acts as a front-end),
this to keep backwards compatibility.

HTH,
Greetings

Thanks; that helps a lot.
Robin S.
 
A

Andrea Caldarone

You can bind to a DataRelation, example :
parentDataGrid.DataSource = dsMain;
parentDataGrid.DataMember = "Clienti";

childDataGrid.DataSource = dsMain;
childDataGrid.DataMember = "Clienti.Clienti_Indirizzi";

- OR -

parentDataGrid.DataSource = dsMain.Tables["Clienti"];

childDataGrid.DataSource = dsMain.Tables["Clienti"];
childDataGrid.DataMember = "Clienti_Indirizzi";


That's basicly the same but don't mix them. For relations to work :

- the child DataGrid needs to use the exact same DataSource as the
parent DataGrid,

- the child DataGrid needs to use the same DataMember as the parent
DataGrid with the relation name appended,

- both DataGrid's need the same BindingContext, but that's the default
anyway

This will automatically filter the child DataGrid when you browse the
parent DataGrid, and it will also automatically insert the parent
keys into child records.


HTH,
Greetings

Thanks all

Yes, this works fine, but how can I hide certain column of the child
datatable when it is displayed into the child datagrid?
 
B

Bart Mermuys

Hi,

Andrea Caldarone said:
You can bind to a DataRelation, example :

parentDataGrid.DataSource = dsMain;
parentDataGrid.DataMember = "Clienti";

childDataGrid.DataSource = dsMain;
childDataGrid.DataMember = "Clienti.Clienti_Indirizzi";

- OR -

parentDataGrid.DataSource = dsMain.Tables["Clienti"];

childDataGrid.DataSource = dsMain.Tables["Clienti"];
childDataGrid.DataMember = "Clienti_Indirizzi";


That's basicly the same but don't mix them. For relations to work :

- the child DataGrid needs to use the exact same DataSource as the
parent DataGrid,

- the child DataGrid needs to use the same DataMember as the parent
DataGrid with the relation name appended,

- both DataGrid's need the same BindingContext, but that's the default
anyway

This will automatically filter the child DataGrid when you browse the
parent DataGrid, and it will also automatically insert the parent
keys into child records.


HTH,
Greetings

Thanks all

Yes, this works fine, but how can I hide certain column of the child
datatable when it is displayed into the child datagrid?

Hide a column in the child DataGrid, i suppose it should work like hidding
any other DataGrid column:

1) set the relevant DataColumn.ColumnMapping to MappingType.Hidden
or
2) create a custom DataGridTableStyle and create DataGridColumnStyles for
the columns you do want to see.


HTH,
Greetings
 

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