Master Detail Relation. Get all Child Tables

Master Detail Relation. Get all Child Tables

Postby chris901 » Mon 31 Oct 2016 13:40

Hello,

when I establish a master-child relation I set the ParentRelation property of the child PgSqlDataTable.

Afaik there is no property that relates from the Parent to the Child. It is only set from the Child to the Parent.

Is there a way I can iterate through all Childs that were set for a Parent table?

E.g. 3 tables

table1 is parent table of table2 and table3.

Now I want to list all the childs like this:

ListChilds(PgSqlDataTable table)
{
}

should output: table2 and table3
chris901
 
Posts: 64
Joined: Wed 20 Jul 2016 04:21

Re: Master Detail Relation. Get all Child Tables

Postby Pinturiccio » Tue 01 Nov 2016 15:14

PgSqlDataTable does not know if it has children. PgSqlDataTable knows only if it has a parent table.

However, you can use PgSqlDatSet. PgSqlDatSet contains tables and relations. For example, suppose you have table1 which is a parent of table2 and table3. If all these tables are created in PgSqlDataSet, and the corresponding relations are created, you can use the Relations collection. Check the ParentTable property for each of the Relations element. If it is equal to table1, you can get a value of the ChildTable property. After enumerating all the collection, you can get all the child tables of your table.

The code can look like the following:
Code: Select all
List<PgSqlDataTable> children = new List<PgSqlDataTable>();
foreach(DataRelation relation in pgSqlDataSet1.Relations)
{
   if (relation.ParentTable == pgSqlDataSet1.Tables["table1"]) children.Add((PgSqlDataTable)relation.ChildTable);
}
Pinturiccio
Devart Team
 
Posts: 1978
Joined: Wed 02 Nov 2011 09:44

Re: Master Detail Relation. Get all Child Tables

Postby chris901 » Wed 02 Nov 2016 13:34

Thanks, that works!
chris901
 
Posts: 64
Joined: Wed 20 Jul 2016 04:21


Return to dotConnect for PostgreSQL