View previous topic :: View next topic |
Author |
Message |
jmihelich
Joined: 18 May 2012 Posts: 4
|
Posted: Fri May 18, 2012 2:24 am Post subject: DataSet Table with multiple child relations |
|
|
I have a DataSet which has many tables. I have one table (T2) which has multiple relations to child tables
T1 -> T2 -> T3 -> T6
| -> T4 -> T7
| -> T5 -> T8
However, when I bind to T1 I only see
T1 -> T2 -> T3 -> T6
The following relations are not available.
-> T4 -> T7
-> T5 -> T8
T3,T4,T5 all have relations to the same data in T2.
Is there a way to get those nodes to show up in the virtual tree or is there a way to re-structure the dataset to allow the virtual tree to represent data this way? |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Fri May 18, 2012 9:42 am Post subject: |
|
|
The standard DataSet binding mechanism only supports setting the children to a single relation. To implement binding to multiple child relations you would have to handle the GetChildren event (either for the T2 RowBinding or the tree as a whole) and set the Children collection programmatically as a composite collection of children from each relation. So you would add all of the child rows from each relation to a single collection so that the children of T2 would be first the rows from T3 then the rows from T4 then the rows from T5. _________________ Infralution Support |
|
Back to top |
|
|
jmihelich
Joined: 18 May 2012 Posts: 4
|
Posted: Fri May 18, 2012 4:46 pm Post subject: Results |
|
|
I got it working, thanks. Here's the code for others who may come across the same issue. Another item i had to watch out for to disable update notification to the dataset until the data was consistant by using .BeginLoadData() and .EndLoadData().
Code: |
private void virtualTree1_GetChildren(object sender, GetChildrenEventArgs e)
{
DataRow dr = e.Row.Item as DataRow;
if (dr != null)
{
DataRelationCollection relations = dr.Table.ChildRelations;
if (relations.Count == 0)
{
RowBinding binding = virtualTree1.GetBindingForRow(e.Row);
e.Children = binding.GetChildrenForRow(e.Row);
}
else
{
DataRowView drv = DataRowRowBinding.GetDataRowView(dr);
ArrayList child_rows = new ArrayList();
foreach (DataRelation r in relations)
{
DataRow[] rows = drv.Row.GetChildRows(r);
foreach (DataRow rx in rows)
{
child_rows.Add(rx);
}
}
e.Children = child_rows;
}
}
else // for the table just call the default binding method
{
RowBinding binding = virtualTree1.GetBindingForRow(e.Row);
e.Children = binding.GetChildrenForRow(e.Row);
}
}
|
|
|
Back to top |
|
|
jmihelich
Joined: 18 May 2012 Posts: 4
|
Posted: Fri May 18, 2012 6:43 pm Post subject: A problem with dataset row deletion |
|
|
I have noticed that in some cases when i delete data from my dataset, where the rows are expanded on the virtual tree, those rows do not get removed from the virtual tree.
For example, let say I have the full tree expanded
T1 -> T2 -> T3 -> T6
...........| -> T4 -> T7
...........| -> T5 -> T8
Then in the dataset i delete a row from T3. I get lots of error (for each row and child row it appears):
Error: This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.
VirtualTree: Error while getting field (t3_id) of table (T3).
The row reports its state as DataRowState.Detached in my virtualTree1_GetChildren so there is no data and this node is irrelivant.
Then the nodes still show up in the tree, but with no text/data.
My database does cascade deletes, could that cause the problem?
Is there a way to, in the GetChildren to remove this (and all child) rows(s)? |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Fri May 18, 2012 10:39 pm Post subject: |
|
|
I think this error is probably a result of you handling the combined children collection yorself. Normally Virtual Tree attaches to the ListChanged event of the relation DataView and so is notified when child rows are deleted and can update the tree. In your case you could handle this in two ways. The simplest would be to call VirtualTree.UpdateRows after you delete rows. The second way would be to attach a handler to the ListChanged events of each of the child relation DataView and in the handler call Row.UpdateRows on the parent VirtualTree row. _________________ Infralution Support |
|
Back to top |
|
|
|