Tuesday, May 24, 2011

Bind TreeView in Asp.NET

Using below code you can bind/show treeview in asp.net

for stored procedure: http://dotnetacademy.blogspot.com/2011/05/get-all-child-nodes-of-parent-in-self.html


 private void buildTree()
        {
            List cml;
            cml = cmf.GetAll();
            TVcategory.Nodes.Clear();
            AddNodes(0, TVcategory.Nodes, cml);
        }
        void AddNodes(int id, TreeNodeCollection tn, List cml)
        {

            foreach (Categorymaster cm in cml.Where(s => s.Parentid == id))
            {
                //TreeNode sub = new TreeNode("" + cm.Category_name + "", cm.Category_id.ToString());
                TreeNode sub = createTree(cm);
                tn.Add(sub);
                AddNodes(Convert.ToInt32(sub.Value), sub.ChildNodes, cml);
            }
        }
        private TreeNode createTree(Categorymaster cm)
        {


            TreeNode tn = new TreeNode("" + cm.Category_name + "", cm.Category_id.ToString());

            if (catid > 0)
            {
                if (cm.Category_id == catid)
                {
                    tn.Selected = true;
                }

            }

            return tn;

        }

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates