Click here to Skip to main content
15,891,871 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i wanna show may recursive table thats name is Location and has 4 colum ns"ID,Title,LatinTitle,ParentID" in tree view.

in web and I drag and drop a treeview1 what shall i do? please help me i'm learner
Posted
Updated 5-Aug-12 1:17am
v3
Comments
OriginalGriff 5-Aug-12 3:18am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
For example, what do you mean by "recursive table"?
What environment are you writing for? Web? Winforms? WPF?
Give us details, or we can't really help you much!
Use the "Improve question" widget to edit your question and provide better information.
Tarun Mangukiya 5-Aug-12 3:39am    
OriginalGriff is right.
Please explain in brief...
Mina Mansouri 5-Aug-12 3:39am    
I told WEB!! I have one table in sql 2008r2 its name is Location and has 4 colum ns"ID,Title,LatinTitle,ParentID" i wanna show this in tree view in web .is it clear? Ido not know how can i write its code.thanks alot
Mina Mansouri 5-Aug-12 3:40am    
i tried this http://www.dotnetspider.com/resources/28676-Populate-TreeView-From-DataBase-Using-Recursive.aspx
but it did not help me
Mina Mansouri 5-Aug-12 3:52am    
I would appreciate you guys :( heeeeelllllllppppp meeeeeeeee :((

Here is the code I used to make a treeview model for upto multilevel hierarchy using a single recursive function.
It can be easily modified to use as required.

private List<TreeViewItemModel> MakeTree(IEnumerable<MyObject> list, TreeViewItemModel parentNode)
        {
            List<TreeViewItemModel> treeViewList = new List<TreeViewItemModel>();
            var nodes = list.Where(x => parentNode == null ? x.ParentID == 0 : x.ParentID == int.Parse(parentNode.Id));
            foreach (var node in nodes)
            {
                TreeViewItemModel newNode = new TreeViewItemModel();
                newNode.Id = node.ID.ToString();
                newNode.Text = node.Text;

                if (parentNode == null)
                {
                    treeViewList.Add(newNode);
                }
                else
                {
                    parentNode.Items.Add(newNode);
                }
                BindTree(list, newNode);
            }
            return treeViewList;
        }
 
Share this answer
 
The Example at Populate TreeView From DataBase[^] by Moinul Islam[^] is working just fine,I just checked it.. This is the modified code according to my local table that i used..
C#
public SqlConnection con = new SqlConnection();    
    protected void Page_Load(object sender, EventArgs e)
    {
        Load_tree();
    }
    
    protected DataSet PDataset(string select_statement)
    {
        Connect();
        con.Open();
        SqlDataAdapter ad = new SqlDataAdapter(select_statement, con);
        DataSet ds = new DataSet();
        ad.Fill(ds);
        con.Close();
        return ds;
    }

    public void Connect()
    {
        con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["cnstring"].ConnectionString);
    }

    public void Load_tree()
    {
        DataSet PrSet = PDataset("SELECT * FROM tbl_WebMenu");
        TreeView1.Nodes.Clear();
        foreach (DataRow dr in PrSet.Tables[0].Rows)
        {
            if ((int)dr["ParentID"] == 0)
            {
                TreeNode tnParent = new TreeNode();
                tnParent.Text = dr["MenuName"].ToString();
                string value = dr["MenuID"].ToString();
                tnParent.Expand();
                TreeView1.Nodes.Add(tnParent);
                FillChild(tnParent, value);
            }
        }
    }

    public int FillChild(TreeNode parent, string IID)
    {
        DataSet ds = PDataset("SELECT * FROM tbl_WebMenu WHERE ParentID =" + IID);
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                TreeNode child = new TreeNode();
                child.Text = dr["MenuName"].ToString().Trim();
                string temp = dr["MenuID"].ToString();
                child.Collapse();
                parent.ChildNodes.Add(child);
                FillChild(child, temp);
            }
            return 0;
        }
        else
        {
            return 0;
        }
    }


If you want something else state it clearly..e.g "Recursive table"?!.. if you mean following structure
HTML
Parent 1
    -- Sub-Parent
         --Child
Parent 2
    -- Child
Parent 3
Parent 4
    -- Child 1
    -- Child 2

the above code by Moinul Islam is for you.. If not then explain "Recursive table"
 
Share this answer
 
Comments
Mina Mansouri 7-Aug-12 1:22am    
thank you so much my friend :) i tried that but did not work. i tried something else and it was ok.thanks for your help :X
Mina Mansouri 8-Aug-12 2:40am    
Hi i wanna put check boxes inside them.what can i do??? tnx 4 ur help
Member 13958044 29-Sep-18 12:49pm    
Thank guy so much
sk-seo 18-Oct-20 10:42am    
That solution made my day :-)
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900