Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

treeview treeNodes

mascatulmascatul Posts: 4Member
Hello guys,
I very new in C# and i started building an app for my company.

Overall it haves a lot of functions one of them it showing treeview populated from a database. Im populating the tree at every mouseclick on the treeNode. The nodes are references in DB to some physical files on a fileserver. The problem comes at reverse, building the tree based on the files.
At reverse i need to build the tree based on the query result.
It seems that it should be like this: Get the root node expand > get and add child's expand > from the child's select the one i need expand > add child's > from here select the one that i need .... and so on . I dont know how to do this from code without mouse events.


Comments

  • DataDinkDataDink Posts: 135Member
    I think what you are saying is you need to dynamically load the entire tree structure without having a user interaction?

    For this you need to use a programming technique called recursion. This means you need to build a method that calls itself in order to keep itterating through all of the children of a hierarchy of items.

    The idea is to build a method that can go through a 1-dimensional list, that calls itself to navigate through the children (if any) of each item in that list. By doing that it will travel down the chain of every child into infinity (or until your computer runs out of memory or stack space) and still be able to pick up where it left off from after the last child has been navigated.


    try this:
    [code]
    public void LoadTreeNodes(string path, TreeNodeCollection nodes)
    {
    var files = Directory.GetFiles(path);
    var dirs = Directory.GetDirectories(path);
    foreach (var file in files)
    nodes.Add(file);
    foreach (var dir in dirs)
    {
    var dnode = nodes.Add(dir);
    LoadTreeNodes(dir, dnode.Nodes);
    }
    }

    [/code]

    Then using the "Nodes" property of your treeview you can do this...
    LoadTreeNodes("C:\Programs\", TreeView1.Nodes);

    and poof - the entire hierarchy of C:Programs will be loaded into your tree.

    You -could- potentially do this on a hierarchy as large as your entire C drive - but it might take a while to load all that info into your tree view.
  • mascatulmascatul Posts: 4Member
    yes you are right but actually my problem is that i dont know how to find a node in the treeview and select it so i can work on further on it.
    My algorithm should be :

    1. Get Rood.
    2. Get Child.
    3. Find a node from the result of 2.
    4. Get child of the result of 3
    and so on ..
    Any suggestions ...?

  • DataDinkDataDink Posts: 135Member
    I don't think I'm completely understanding what you're asking, sorry - are you wanting to navigate the node hierarchy of a tree view? Are you looking for a way to flatten the hierarchy so you can deal with it 1-dimensionally?
  • mascatulmascatul Posts: 4Member
    Ok i want populate the treeview programmatically.My problem is :
    After i have the root i have to find it's children's from the childrens identify one of them get his subnodes and expand it. Every thing it's ok when i use the mouse to select what node i want to expand, but for example when i want to find one of the subnodes and work on it i have a problem.
    [code]
    public void getChild()
    {
    string query = "SELECT id,name,type,sub_type,locker,deleted,owner,creation_date,modification_date FROM dbo.v_accessible_nodes with (nolock) where deleted=0 and parent_id='" + ParentNode.Tag + "' order by [type], [order]";
    sqlConnection = new SqlConnection(this.ConnectionString + ConnectionStringDB);
    SqlCommand sqlquery = new SqlCommand(query, sqlConnection);
    SqlDataAdapter DatabasesDA = new SqlDataAdapter(query, sqlConnection);
    DataTable DatabasesDT = new DataTable();
    DatabasesDA.Fill(DatabasesDT);
    for (int i = 0; i < DatabasesDT.Rows.Count; i++)
    {
    TreeNode ChildNode = new TreeNode();
    ChildNode.Text = DatabasesDT.Rows[i]["name"].ToString();
    ChildNode.Name = DatabasesDT.Rows[i]["sub_type"].ToString();
    ChildNode.Tag = DatabasesDT.Rows[i]["id"].ToString();


    CurrentNode.Nodes.Add(ChildNode);
    }

    sqlConnection.Close();
    }
    private void GetNode(TreeNode eNode)
    {
    CurrentNode = eNode;
    CurrentNode.SelectedImageIndex = CurrentNode.ImageIndex;
    ParentNode.Tag = eNode.Tag;
    if (CurrentNode.Nodes.Count > 0 || CurrentNode.Name == "16")
    {
    return;
    }
    else
    {
    try
    {
    if (CurrentNode.Name == "10")
    {
    getFiles();
    getChild();
    CurrentNode.Expand();
    }
    else
    {
    getChild();
    CurrentNode.Expand();
    }

    }
    catch (Exception)
    {
    }
    }
    }
    [/code]
    The code shows how i populate the tree by mouse click.

    after this i have another method that get from the db a hierarchical path for this tree. The idea was to expand the nodes based on this path. For example i have the root added under his children search for the first node of my path and expand it after this from the expanded node get the matching child that it's identical with my second node from my resulted path and so on. My problems are finding the node compare it and expand it .If you really dont understand i'l post my whole class
  • DataDinkDataDink Posts: 135Member
    Sorry if I'm a bad teacher or am not explaining things correctly but I still think you are looking for something "recursive".

    so like this would be a real dirty example of what that would be for you...
    NOTE: I'm putting "..." in places that I don't want to type out some details
    [code]
    public void LoadUpNodes(TreeNodeCollection collection, string parentId)
    {
    // Get a list of children from the database...
    query = "SELECT ... WHERE parent_id=" + parentId + ";";
    connection = new SqlConnection(...
    command = connection.CreateCommand();
    command.CommandText = query;
    reader = command.ExecuteReader();

    // Go through each child that belongs to the specified parentId...
    while (reader.Read())
    {
    // add this child to the given treenode collection
    TreeNode newChild = collection.Add(reader["name"], reader["somethingelse"]);

    // Now here is where recursion comes in:
    // You can call this same function to load all of the children of
    // this child node. By doing so, then in turn this function will
    // be called again to load the children of each child of this
    // child node. And then again for each child of each child of
    // each child of each child and so on...
    LoadUpNodes(newChild.Nodes, reader["id"]);

    // Now, once the entire hierarchy for this child has been loaded
    // this will continue loading the top most children and all of
    // their hierarchies, right here where you left off...

    // Note that each child "TreeNode" has a ".Nodes" property where
    // its children are stored. By passing that .Nodes property to
    // this function along with the id of this child, then "this"
    // child will be loaded with all nodes having a parent id of "this"
    // child.
    }
    }
    [/code]

    So you would start by taking your root-most id - I"m going to pretend that is "0" and call the recursive method...
    LoadUpNodes(myTreeView.Nodes, "0");

    So the "LoadUpNodes" method get's all of the children of "0".

    When it goes to add the first child of "0" - let's pretend its ID is "1", then it calls itself like so:

    LoadUpNodes(newChild.Nodes, "1");

    Now it searches for all children of "1" and loades them into the node that is associated with the record id of "1" - (and doesn't load them to the root tree view.

    Let's say the first child for record id "1" is id "2" - same thing, the function calls itself again:
    LoadUpNodes(yetAnotherChild.Nodes, "2");

    And all of "2"'s children are loaded up. - Now let's say the children of id "2" do not have any children themselves - at this point no more recursive calls would be made to LoadUpNodes and the call that was made to load the children of id "1" would continue to load the next child after id "2".

    EDIT:
    Now - you said that you were having trouble finding the "node" to load children into. I'm hoping this example demonstrates that you actually pass the child node that you created into the method recursively. By passing the child - that child now becomes the one that is having children loaded into it.

    This is real confusing the first time you try to wrap your head around it, but once you get it, it's real easy.

Sign In or Register to comment.