Reputation: 1599
I have a Node class, which is used to store a tree. Text stores the Node value and Children stores the sub trees. I am coverting the tree to a DataTable, by first flattening the tree and saving it in DataTable
public class Node
{
public string Text { get; set; }
public List<Node> Children { get; set; }
}
For Example
1
/ \
2 5
/ \ \
3 4 6
is flattened to
1-2-3
1-2-4
1-5-6
Now I am binding this DataTable to DataGrid. When the user changes the value in DataGrid it is changed in DataTable, but is there any way that it is also changed in my Tree. I am asking if there can be binding between the two classes Node and Datatable.
Upvotes: 0
Views: 117
Reputation: 3255
Your DB structure is a little bit rendundant. IMO you should define objects as a parent key/value pair, so you could edit single values without re-reading the whole tree. It'd also make it easy to get a list of of child items by calling single select query. If values in your tree are distinct, you could threat your values as primary keys. If they can repeat across the tree, then you'd need additional third field to store unique id key.
I'm not sure what kind of DataGrid access are you using, but f.e. with LinqToSql you could create generate yourself a data model context, containing entity definition. Then you can edit it (or derive from it) and add List<Node> Children
as a non-db field with a custom getter, returning you some lazy collection of child items.
Upvotes: 0
Reputation: 69959
I am coverting the tree to a DataTable, by first flattening the tree and saving it in DataTable
Why would you voluntarily make your job more complicated, with no benefits? Don't convert (or 'flatten') your data, don't use a DataTable
to hold it and don't display it in a DataGrid
. You are asking if I flatten my hierarchical data, can changes made to that flattened data be represented in a hierarchical manner? The answer to that is yes... but with a lot of work.
A far better option would be to keep your hierarchical data in its hierarchical form and then display it in a TreeView
, or some other hierarchical manner. Data binding the actual data in its natural form will enable changes to be propagated back to the data elements as expected.
Upvotes: 3