Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Display TreeNodeCollection on a datagrid

Display TreeNodeCollection on a datagrid

Scheduled Pinned Locked Moved ASP.NET
csharpasp-net
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hercules01
    wrote on last edited by
    #1

    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.

    M 1 Reply Last reply
    0
    • H Hercules01

      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.

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      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.

      H 1 Reply Last reply
      0
      • M minhpc_bk

        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.

        H Offline
        H Offline
        Hercules01
        wrote on last edited by
        #3

        Thanks man its working :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups