Display TreeNodeCollection on a datagrid
-
Guys, we sure all know that the ASP.Net Datagrid should display any datasource wich implement the IList interface, although the TreeNodeCollection implement the IList interface but when you assign the the ASP.Net Datagrid datasource property to a TreeNodeCollection it accept it but throws an exception on the DataBind method "Object reference not set to an instance of an object" though the TreeNodeCollection is valid and full of TreeNode's, can any one please explain this behavior, i tried to add TreeNode's on different Levels but still it throw an exception. I'm interested in this case because i'm developing a Data-Bound Control which will accept a TreeNodeCollection as its DataSource.
-
Guys, we sure all know that the ASP.Net Datagrid should display any datasource wich implement the IList interface, although the TreeNodeCollection implement the IList interface but when you assign the the ASP.Net Datagrid datasource property to a TreeNodeCollection it accept it but throws an exception on the DataBind method "Object reference not set to an instance of an object" though the TreeNodeCollection is valid and full of TreeNode's, can any one please explain this behavior, i tried to add TreeNode's on different Levels but still it throw an exception. I'm interested in this case because i'm developing a Data-Bound Control which will accept a TreeNodeCollection as its DataSource.
Hi there, Yes, we can bind a TreeViewCollection to a daragrid. As you know that we use the DataBind method to bind to data source with the data list control, and it normally works in the way that each object (TreeNode) in the list (TreeViewCollection) is bound to a row, and each property of TreeNode is bound to a column of the row. The exception as you said happens at this stage when the Expanded property of a TreeNode is retrieved. In the get method of this property, the code makes reference to the parent (TreeView) which is null, so it throws an exception. To work around this error, there are couple of ways here: + You don't bind this property to the datagrid. For example, set AutoGenerateColumns to false, the columns which are declared don't make reference to this property. + You simply declare a TreeView object for the TreeViewCollection.
...
TreeView tree = new TreeView();
tree.ExpandLevel = 0;TreeNodeCollection col = new TreeNodeCollection(tree);
...+ You modify the source code of the TreeNode class.
-
Hi there, Yes, we can bind a TreeViewCollection to a daragrid. As you know that we use the DataBind method to bind to data source with the data list control, and it normally works in the way that each object (TreeNode) in the list (TreeViewCollection) is bound to a row, and each property of TreeNode is bound to a column of the row. The exception as you said happens at this stage when the Expanded property of a TreeNode is retrieved. In the get method of this property, the code makes reference to the parent (TreeView) which is null, so it throws an exception. To work around this error, there are couple of ways here: + You don't bind this property to the datagrid. For example, set AutoGenerateColumns to false, the columns which are declared don't make reference to this property. + You simply declare a TreeView object for the TreeViewCollection.
...
TreeView tree = new TreeView();
tree.ExpandLevel = 0;TreeNodeCollection col = new TreeNodeCollection(tree);
...+ You modify the source code of the TreeNode class.
Thanks man its working :)