0

I currently have a query which returns a data table into C# which is structured as follows:

ID | ParentID | ParentOfParentID | Name

What I'm looking to do is create a function to link this returned System.Data.DataTable to the ASP.NET treeview so that I get the structure as described - but so far I'm having no luck.

Any help would be appreciated - any treeviews which are 'better' than the ASP one would also be helpful! (As I don't quite like the ASP one).

Thanks

Adam H
  • 1,159
  • 3
  • 15
  • 27
  • Duplicate: http://stackoverflow.com/questions/697285/how-to-bind-asp-net-treeview-control-to-a-datatable. See also http://www.codeproject.com/Articles/140827/Dynamic-Binding-Of-Hierarchy-Data-Structure-To-Tre – Robert Harvey Oct 11 '12 at 17:04
  • My question is a slight twist on those in that I'm dealing with three ID columns - not two. – Adam H Oct 12 '12 at 07:21
  • Can you post sample data from your 3 ID columns? The ParentOfParentID is confusing as to its values. – MJH Oct 14 '12 at 05:26

2 Answers2

1

I wrote a BLOG entry on how to do this:

http://weblogs.asp.net/stevewellens/archive/2009/01/01/from-table-to-treeview-displaying-hierarchies.aspx

I don't know why you have ParentOfParentID, that seems redundant and would make moving nodes to a different parent really tough.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
0

In code below "Node" class represents your one row from database (code is not included to convert database row to Node class object).

TreeView will look like this

<asp:TreeView ID="TreeView1" runat="server" 
onselectednodechanged="TreeView1_SelectedNodeChanged" 
ontreenodecollapsed="TreeView1_TreeNodeCollapsed" 
ontreenodeexpanded="TreeView1_TreeNodeExpanded">
<SelectedNodeStyle BackColor="#FFFFCC" />
</asp:TreeView>

and page behind code is below. Method "getTreeMenu()" returns collection of nodes to display

public class Node
    {
        public int Id;
        public string text;
        public bool IsParent;
        public int parentId;
        public string url;
        public bool IsSelected;
        public bool IsExpanded = false;
    }

    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        List<Node> lstNodes = new List<Node>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              bindTree();
            }
        }

        TreeNode searchResult;
        public void SelectNodesRecursive(string searchValue, TreeView Tv)
        {
            foreach (TreeNode tn in Tv.Nodes)
            {
                if (tn.Value == searchValue)
                {
                    searchResult = tn;
                    break;
                }

                if (tn.ChildNodes.Count > 0)
                {
                    foreach (TreeNode cTn in tn.ChildNodes)
                    {
                        int a = SelectChildrenRecursive(cTn, searchValue);
                        if (a == 1)
                        {
                            searchResult = cTn;
                        }
                    }
                }
            }
        }


        private int SelectChildrenRecursive(TreeNode tn, string searchValue)
        {
            if (tn.Value == searchValue)
            {
                searchResult = tn;
                return 1;
            }

            if (tn.ChildNodes.Count > 0)
            {
                foreach (TreeNode tnC in tn.ChildNodes)
                {
                    int a = SelectChildrenRecursive(tnC, searchValue);
                    if (a == 1)
                    {
                        searchResult = tnC;
                        return 1;
                    }

                }

            }
            searchResult = null;
            return 0;
        } 


        private void bindTree()
        {
            TreeNode parent = null, child = null;

            if (Session["TreeNodes"] == null)
            {
                lstNodes = getTreeMenu();
                Session["TreeNodes"] = lstNodes;
            }
            else
            {
                lstNodes = (List<Node>)Session["TreeNodes"];
            }

            foreach (Node node in lstNodes)
            {
                if (node.IsParent && node.parentId == 0)
                {
                    parent = new TreeNode { Text = node.text, Value = node.Id.ToString()};
                    TreeView1.Nodes.Add(parent);
                    parent.Selected = node.IsSelected;
                    parent.Expanded = node.IsExpanded;
                }
                else
                {
                    if (node.IsParent)
                    {
                        SelectNodesRecursive(node.parentId.ToString(), TreeView1);
                        parent = searchResult;
                        child = new TreeNode { Text = node.text, Value = node.Id.ToString()};
                        parent.ChildNodes.Add(child);
                        child.Selected = node.IsSelected;
                        child.Expanded = node.IsExpanded;
                    }
                    else
                    {
                        child = new TreeNode { Text = node.text, Value = node.Id.ToString()};
                        SelectNodesRecursive(node.parentId.ToString(), TreeView1);
                        parent = searchResult;

                        if (parent != null)
                        {
                            parent.ChildNodes.Add(child);
                            child.Selected = node.IsSelected;
                            child.Expanded = node.IsExpanded;
                        }
                        else
                        {
                            child.Selected = node.IsSelected;
                            child.Expanded = node.IsExpanded;
                        }
                    }
                }
            } 
        }

        private List<Node> getTreeMenu()
        {
            List<Node> treeSource = new List<Node>();
        treeSource.Add(new Node { Id = 1, IsParent = true, parentId = 0, text = "parent1", url = "home.aspx?id=1", IsExpanded = true });
        treeSource.Add(new Node { Id = 2, IsParent = false, parentId = 1, text = "Child 1", url = "page1.aspx" });
        treeSource.Add(new Node { Id = 3, IsParent = false, parentId = 1, text = "Child 2", url = "Page2.aspx" });
        treeSource.Add(new Node { Id = 4, IsParent = false, parentId = 1, text = "Child 3", url = "Page3.aspx" });
        treeSource.Add(new Node { Id = 5, IsParent = true, parentId = 0, text = "parent 2", url = "home.aspx?id=5", IsExpanded = false });
        treeSource.Add(new Node { Id = 6, IsParent = false, parentId = 5, text = "child 1", url = "page1.aspx" });
        treeSource.Add(new Node { Id = 7, IsParent = true, parentId = 5, text = "parent 3", url = "home.aspx?id=7", IsExpanded=false });
        treeSource.Add(new Node { Id = 14, IsParent = false, parentId = 7, text = "child 1", url = "page1.aspx" });
        treeSource.Add(new Node { Id = 15, IsParent = false, parentId = 7, text = "child 2", url = "page2.aspx" });
        treeSource.Add(new Node { Id = 8, IsParent = false, parentId = 5, text = "child 3", url = "Page3.aspx" });
        treeSource.Add(new Node { Id = 9, IsParent = true, parentId = 0, text = "parent 4", url = "home.aspx?id=9", IsExpanded = false });
        treeSource.Add(new Node { Id = 10, IsParent = false, parentId = 9, text = "child 1", url = "page1.aspx" });
        treeSource.Add(new Node { Id = 11, IsParent = false, parentId = 9, text = "child 2", url = "Page2.aspx" });
        treeSource.Add(new Node { Id = 12, IsParent = false, parentId = 9, text = "child 3", url = "Page3.aspx" });
        treeSource.Add(new Node { Id = 13, IsParent = true, parentId = 0, text = "parent 5", url = "About.aspx" });
        return treeSource;
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            string selVal = TreeView1.SelectedValue;
            lstNodes = (List<Node>)Session["TreeNodes"];
            if (lstNodes != null)
            {
                Node nd = lstNodes.Find(n => n.Id.ToString() == selVal);

                foreach (Node item in lstNodes.FindAll(n => n.Id.ToString() != selVal))
                {
                    item.IsSelected = false;
                }

                nd.IsSelected = true;
                Session["TreeNodes"] = lstNodes;
                if (nd.url != "")
                    Response.Redirect(nd.url);
            }
        }

        protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {   
            lstNodes = (List<Node>)Session["TreeNodes"];
            if (lstNodes != null)
            {
                Node nd = lstNodes.Find(n => n.Id.ToString() == e.Node.Value);
                nd.IsExpanded = true;
                Session["TreeNodes"] = lstNodes;
            } 
        }

        protected void TreeView1_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
        {
            lstNodes = (List<Node>)Session["TreeNodes"];

            if (lstNodes != null)
            {
                Node nd = lstNodes.Find(n => n.Id.ToString() == e.Node.Value);
                nd.IsExpanded = false;
                Session["TreeNodes"] = lstNodes;
            }
        }
    }

Let me know if you have any questions

VikasPatil
  • 66
  • 3