Parent expression columns not updating after DataSet merging...

N

Nathan Baulch

When merging a DataTable of fresh data into my DataSet, the expression
columns in my other table that links to that table (using the
PARENT(RELATION).FIELD syntax) are not updated. The only way I have managed
to get it working is using an ugly workaround whereby I re-set the
DataColumn's expression and re-set the DataGrid's source straight after the
merge.

The following code should illustrate my problem. Create a new Form, drop a
DataGrid and two Buttons on it and paste the following code in the
appropriate places.


private DataSet _Set;
private DataTable _Parent;
private DataTable _Child;

private void button1_Click(object sender,EventArgs e)
{
_Parent = new DataTable("PARENT");
_Parent.PrimaryKey = new DataColumn[]
{_Parent.Columns.Add("ID")};
_Parent.Columns.Add("TEXT1");
_Parent.Rows.Add(new object[] {"1","10"});
_Parent.Rows.Add(new object[] {"2","20"});
_Parent.Rows.Add(new object[] {"3","30"});

_Child = new DataTable("CHILD");
_Child.PrimaryKey = new DataColumn[]
{_Child.Columns.Add("ID")};
_Child.Columns.Add("TEXT2");
_Child.Rows.Add(new object[] {"1","one"});
_Child.Rows.Add(new object[] {"2","two"});
_Child.Rows.Add(new object[] {"3","three"});

_Set = new DataSet();
_Set.Tables.Add(_Parent);
_Set.Tables.Add(_Child);
_Set.Relations.Add("PARENTCHILD",
_Parent.PrimaryKey[0],_Child.PrimaryKey[0]);
_Child.Columns.Add("TEXT1",typeof(string),
"PARENT(PARENTCHILD).TEXT1");

dataGrid.DataSource = _Child;
}

private void button2_Click(object sender, System.EventArgs e)
{
DataTable newParent = _Parent.Clone();
newParent.Rows.Add(new object[] {"1","ten"});
newParent.Rows.Add(new object[] {"2","twenty"});
newParent.Rows.Add(new object[] {"3","thirty"});
_Set.Merge(newParent);

//the following 4 lines make up my ugly workaround.
DataColumn col = _Child.Columns["TEXT1"];
col.Expression = col.Expression;
dataGrid.DataSource = null;
dataGrid.DataSource = _Child;
}


Nato
 

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