Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
I have created, treeview with checkboxes, but if i select a folder it export the parent node to a listview/datagridview! but it didn't exporting the subfolders (child nodes). Can anyone give me idea to solve this?

What I have tried:

C#
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Nodes.Count > 0)
            {
                if (e.Node.Nodes[0].Text == "..." && e.Node.Nodes[0].Tag == null)
                {
                    e.Node.Nodes.Clear();


                    string[] dirs = Directory.GetDirectories(e.Node.Tag.ToString());

                    foreach (string dir in dirs)
                    {
                        DirectoryInfo di = new DirectoryInfo(dir);
                        TreeNode node = new TreeNode(di.Name, 0, 1) { Checked = true };

                        try
                        {
                            node.Tag = dir;


                            if (di.GetDirectories().Count() > 0)
                                node.Nodes.Add(null, "...", 0, 0).Checked = node.Checked;
                        }
                        catch (UnauthorizedAccessException)
                        {
                            node.ImageIndex = 12;
                            node.SelectedImageIndex = 12;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Tree_to_Grid", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                        }
                        finally
                        {
                            node.Checked = e.Node.Checked;
                            e.Node.Nodes.Add(node);
                        }
                    }
                }
            }
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            button1.Enabled = false;
            TreeNode node = e.Node;
            bool is_checked = node.Checked;
            foreach (TreeNode childNode in e.Node.Nodes)
            {
                childNode.Checked = e.Node.Checked;
            }
            treeView1.SelectedNode = node;   
        }
Posted
Updated 28-Apr-16 21:09pm
v3
Comments
Sergey Alexandrovich Kryukov 29-Apr-16 2:52am    
Idea? Sure: the debugger.

You did not really explain what you want to achieve. Apparently, this is not a copy of folders. It looks more like virtual tree view which shows file system objects as you browse the tree...

—SA

1 solution

When you tick any parent node, its child nodes also gets ticked automatically, you just need to copy them.
checkout below code, which gives you selected node of treeview
C#
List<string> CheckedNames( System.Windows.Forms.TreeNodeCollection theNodes)
{
    List<string> aResult = new List<string>();

    if ( theNodes != null )
    {
        foreach ( System.Windows.Forms.TreeNode aNode in theNodes )
        {
            if ( aNode.Checked )
            {
                aResult.Add( aNode.Text );
            }

            aResult.AddRange( CheckedNames( aNode.Nodes ) );
        }
    }

    return aResult;
}</string></string></string>
 
Share this answer
 
Comments
Santosh Kokatnur 29-Apr-16 3:19am    
I have added this in my current application already!
koolprasad2003 29-Apr-16 3:26am    
so what is the issue, just you need to collect the checked node and copy them with System.IO namespace.
Santosh Kokatnur 29-Apr-16 3:28am    
Ya! I want to copy all the treeview parent node as well as childnode to datagridview!
koolprasad2003 29-Apr-16 3:31am    
iterate all node and fill data table with it, then directly bind the datatable to gridview as datasource
Santosh Kokatnur 29-Apr-16 3:33am    
Thank you bro :)

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