Master Detail Relation. Get all Child Tables

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
chris901
Posts: 64
Joined: Wed 20 Jul 2016 04:21

Master Detail Relation. Get all Child Tables

Post by 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

Pinturiccio
Devart Team
Posts: 2192
Joined: Wed 02 Nov 2011 09:44

Re: Master Detail Relation. Get all Child Tables

Post by 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);
}

chris901
Posts: 64
Joined: Wed 20 Jul 2016 04:21

Re: Master Detail Relation. Get all Child Tables

Post by chris901 » Wed 02 Nov 2016 13:34

Thanks, that works!

Post Reply