DataSet DataTable Relation Trouble

I have a DataSet...

With these tables: Parent, Child, and GrandChild.

Each table has an Id, ParentId and Name field. (except the top Parent table)

With these Relations:
ds.Relations.Add("ParentToChild",ds.Tables["Parents"].Columns["Id"],ds.Tables["Children"].Columns["ParentId"],true);
ds.Relations.Add("ChildToGrandChild",ds.Tables["Children"].Columns["Id"],ds.Tables["GrandChildren"].Columns["ChildId"],true);

And these "related columns":
ds.Tables["Children"].Columns.Add("ParentName",typeof(string),"Parent(ParentToChild).Name");
ds.Tables["GrandChildren"].Columns.Add("GrandParentName",typeof(string),"Parent(ChildToGrandChild).ParentName");
 
If I add a bunch of parents, children, and grandchildren test data everything works fine so far.  If I then change the data in a parent's Name field, it changes the ParentName field in the "Children" datatable naturally, however it will not change the data in the "GrandChildren"'s GrandParentName field.  What am I doing wrong here?  Why won't the Parent's name change reflect all the way down to the GrandChildren datatable?  It reflects to the "Children", why wont it go the next step?
 
Below is a the complete Windows Forms code, using a DataGrid to visually see this happen...
 
 

using System;

using System.Windows.Forms;

using System.Data;

namespace DataSetTest

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.DataGrid dataGrid1;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.dataGrid1 = new System.Windows.Forms.DataGrid();

((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();

this.SuspendLayout();

//

// dataGrid1

//

this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)

| System.Windows.Forms.AnchorStyles.Left)

| System.Windows.Forms.AnchorStyles.Right)));

this.dataGrid1.DataMember = "";

this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;

this.dataGrid1.Location = new System.Drawing.Point(8, 8);

this.dataGrid1.Name = "dataGrid1";

this.dataGrid1.Size = new System.Drawing.Size(280, 248);

this.dataGrid1.TabIndex = 0;

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 266);

this.Controls.Add(this.dataGrid1);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);

((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_Load(object sender, System.EventArgs e)

{

DataSet ds = new DataSet("TestDataSet");

ds.Tables.Add(ParentTable());

ds.Tables.Add(ChildTable());

ds.Tables.Add(GrandChildTable());

ds.Relations.Add("ParentToChild",ds.Tables["Parents"].Columns["Id"],ds.Tables["Children"].Columns["ParentId"],true);

ds.Relations.Add("ChildToGrandChild",ds.Tables["Children"].Columns["Id"],ds.Tables["GrandChildren"].Columns["ChildId"],true);

ds.Tables["Children"].Columns.Add("ParentName",typeof(string),"Parent(ParentToChild).Name");

ds.Tables["GrandChildren"].Columns.Add("GrandParentName",typeof(string),"Parent(ChildToGrandChild).ParentName");

 

dataGrid1.DataSource = ds;

}

private DataTable ParentTable()

{

DataTable dt = new DataTable("Parents");

DataColumn dc = new DataColumn("Id");

dt.Columns.Add(dc);

dc = new DataColumn("Name");

dt.Columns.Add(dc);

return dt;

}

private DataTable ChildTable()

{

DataTable dt = new DataTable("Children");

DataColumn dc = new DataColumn("Id");

dt.Columns.Add(dc);

dc = new DataColumn("ParentId");

dt.Columns.Add(dc);

dc = new DataColumn("Name");

dt.Columns.Add(dc);

return dt;

}

private DataTable GrandChildTable()

{

DataTable dt = new DataTable("GrandChildren");

DataColumn dc = new DataColumn("Id");

dt.Columns.Add(dc);

dc = new DataColumn("ChildId");

dt.Columns.Add(dc);

dc = new DataColumn("Name");

dt.Columns.Add(dc);

return dt;

}

}

}

1 Comment

  • A DataSet represents a database. The Tables are tables, the relations are the relations between the (data)tables. It means only that specific data will change, which have bene changed "manually" - as it happens in a database. The .Net framework - like an Sql server - responseble only for keeping integrity of data modell.

Comments have been disabled for this content.