Parent Child grandchild relation problems

  • Thread starter Thread starter Henry
  • Start date Start date
H

Henry

I got a dataset with three tables 'Parent', 'Child' and 'Grandchild'.
The relation will *work* on "Parent" and "child". KEYs from "Parent"
will cascade to "Child" without any problem. However "Child" and
"Grandchild" relation won't work. KEYs in the "Child" table only
cascade once to the "Grandchild" table. Subsequence records from
"Child" won't cascade to the "Grandchild" table. I bind those three
tables into three separate DataGrid. Attach are the dataset and
relation I am using right now. I really appreciate anyone can help
figure this out... Thanks :)


Henry


//*********************
//Parent Child DataGrid
//*********************

ParentDataGrid.DataSource = ds;
ParentDataGrid.DataMember="Parent";

ChildDataGrid.DataSource = ds;
ChildDataGrid.DataMember="ds.Parent_Child";

GrandChildDataGrid.DataSource = ds;
GrandChildDataGrid.DataMember="ds.Child_GrandChild";


//*********************
//DATASET
//*********************

tbl = ds.Tables.Add("Parent");
col = tbl.Columns.Add("ParentID", typeof(System.Int32));
col.AllowDBNull = false;
col.AutoIncrement = true;
col.AutoIncrementSeed = -1;
col.AutoIncrementStep = -1;
col.ReadOnly = true;
col.Unique = true; tbl.PrimaryKey = new DataColumn[]
{tbl.Columns["ParentID"]};

tbl = ds.Tables.Add("Child");
col = tbl.Columns.Add("ChildID", typeof(System.Int32));
col.AllowDBNull = false;
col.AutoIncrement = true;
col.AutoIncrementSeed = -1;
col.AutoIncrementStep = -1;
col.ReadOnly = true; col.Unique = true;
col = tbl.Columns.Add("ParentID", typeof(System.Int32));
col.AllowDBNull = false;
tbl.PrimaryKey = new DataColumn[] {tbl.Columns["ChildID"]};

tbl = ds.Tables.Add("GrandChild");
col = tbl.Columns.Add("GrandChildID", typeof(System.Int32));
col.AllowDBNull = false;
col.AutoIncrement = true;
col.AutoIncrementSeed = -1;
col.AutoIncrementStep = -1;
col.ReadOnly = true;
col.Unique = true;
col = tbl.Columns.Add("ChildID", typeof(System.Int32));
col.AllowDBNull = false;
tbl.PrimaryKey = new DataColumn[] {tbl.Columns["GrandChildID"]};

//*********************
//Data Relation
//*********************
ds.Relations.Add("Parent_Child",ds.Tables["Parent"].Columns["ParentID"],ds.Tables["Child"].Columns["ParentID"]);

ds.Relations.Add("Child_GrandChild",ds.Tables["Child"].Columns["ChildID"],ds.Tables["GrandChild"].Columns["ChildID"]);
 
I think the way you bound the 3 datagrids are not correct. If you have the following datarelations:

ds.Relations.Add(New DataRelation("CustOrder", ds.Tables("Customers").Columns("CustomerID"), ds.Tables("Orders").Columns("CustomerID")))
ds.Relations.Add(New DataRelation("OrderOrdDetails", ds.Tables("Orders").Columns("OrderID"), ds.Tables("OrderDetails").Columns("OrderID")))

then you bind the 3 datagrids as:

DataGrid1.DataSource = ds.Tables("Customers")
DataGrid2.DataSource = ds.Tables("Customers")
DataGrid2.DataMember = "CustOrder"
DataGrid3.DataSource = ds.Tables("Customers")
DataGrid3.DataMember = "CustOrder.OrderOrdDetails"

Note that :
- The DataSource property is the same for all datagrids, and
- The DataGrid3's DataMemeber property should be bound to the first datarelation dot the second datarelation.

I hope this helps!


Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.
 
Back
Top