Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a treeview with logical drivers with checkboxes, Now i have to display treeview selected folders(if it is having subfolders and files also) display in datagridview. I have tried but not getting.

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, "File Explorer", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                        }
                        finally
                        {
                            node.Checked = e.Node.Checked;
                            e.Node.Nodes.Add(node);
                        }
                    }
                }
            }                   
           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (TreeNode originalNode in trwFileExplorer.Nodes)
            {
                 TreeNode newNode = new TreeNode(originalNode.FullPath);
                 newNode.Tag = originalNode.Tag;
                 treeView2.Nodes.Add(newNode);
                 IterateTreeNodes(originalNode, newNode);


            }
        }

        private void IterateTreeNodes(TreeNode originalNode, TreeNode rootNode)
        {
            foreach (TreeNode childNode in originalNode.Nodes)
            {
                TreeNode newNode = new TreeNode(childNode.FullPath);
                newNode.Tag = childNode.Tag;
                treeView2.SelectedNode = rootNode;
                treeView2.SelectedNode.Nodes.Add(newNode);
                IterateTreeNodes(childNode, newNode);
            }
        }


I haven't taken datagridview to display here. instead of datagridview I took treeview2. but I want to display its full selected nodes full path in datagridview.
Posted
Updated 16-May-16 21:44pm
v6

Why not a use a prefix in your tags like this:
C#
e.Node.Tag = @"dir:c:\temp"

Then you could easily recognize it like this:
C#
if (e.Node.Tag.ToString().StartsWith("dir:"))
 
Share this answer
 
v5
Comments
Santosh Kokatnur 16-May-16 3:30am    
e.Node.Nodes[0].Tag == null

Its correct. if I add your solution means, Subfolders are not at all showing!
RickZeeland 16-May-16 3:39am    
I will update my solution, just wait a few secs ...
Santosh Kokatnur 16-May-16 3:48am    
Object does not contain definition for "Text".. ERROR
RickZeeland 16-May-16 3:55am    
Yes, yes, I will correct that, but what do you think about the idea ?
Santosh Kokatnur 16-May-16 4:01am    
My question is: I need to display selected nodes in datagridview.
Example for BindingList using Tree nodes.
You could even use:
C#
public BindingList<TreeNode> bindingList;

but that's not very practical :)
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace TestForm1
{
    /// <summary>
    /// Test data grid with BindingList.
    /// </summary>
    public partial class Form8 : Form
    {
        public BindingList<diskClass> bindingList;

        public Form8()
        {
            InitializeComponent();

            // Allow user to resize column widths.
            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
            this.dataGridView1.AllowUserToAddRows = false;
        }

        /// <summary>
        /// Fill the BindingList and set the dataGridView1.DataSource.
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            bindingList = new BindingList<diskClass>();
            bindingList.AllowNew = true;
            bindingList.AllowRemove = true;

            if (this.dataGridView1.DataSource != null)
            {
                this.dataGridView1.DataSource = null;
            }

            this.AddTestData(@"C:\Temp");
         }

        /// <summary>
        /// Add rows with directory names.
        /// </summary>
        private void AddTestData(string dirString)
        {
            string[] dirs = Directory.GetDirectories(dirString);
            int number = 1;

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

                var item = new diskClass();
                item.Number = number++;
                item.Name = node.Name;
                item.Path = node.Text;
                bindingList.Add(item);
            }

            this.dataGridView1.DataSource = bindingList;
            this.dataGridView1.Columns[0].Frozen = true;
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                this.bindingList.RemoveAt(this.dataGridView1.CurrentRow.Index);
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            var item = new diskClass();
            bindingList.Add(item);
        }
    }

    public class diskClass
    {
        public int Number { get; set; }
        public string Name { get; set; }
        public string Path { get; set; }
    }
}
 
Share this answer
 
v5
Comments
Santosh Kokatnur 16-May-16 9:21am    
Is it code for local disks data binding?
RickZeeland 16-May-16 9:25am    
No, it's just an example how you can use BindingList and add data to it.
You can change the dgvClass1 to suit your needs, maybe you only need one property "Name".
Santosh Kokatnur 16-May-16 9:28am    
I need, Treeview Selected nodes in datagridview with full path...
RickZeeland 16-May-16 10:12am    
See updated example with Tree nodes ...
Santosh Kokatnur 16-May-16 10:16am    
Ofcourse, Will try it... Thanks:)

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