how to add sub child in Tree View in c# win form?

Farshad Valizade 501 Reputation points
2024-11-11T12:42:21.88+00:00

hi

I have this kind of data in my data table.

now in tree view I want to show it.

I have used this code but it just put every report no in the first no

please look at it.

bbbbb

aaa

my code:

        private void Form3_Load(object sender, EventArgs e)
        {
            //Load Combo Line
            comboLineno.DataSource = LineBLL.GetAllLineNoWithId();
            comboLineno.DisplayMember = "LineNo";
            comboLineno.ValueMember = "LineId";
            comboLineno.SelectedIndex = -1;
            // Programmatically adding ImageList
            ImageList imageList = new ImageList();
            imageList.Images.Add("parent", Properties.Resources.file_explorer_20px);
            imageList.Images.Add("child", Properties.Resources.folder_16px);
            imageList.Images.Add("subchild", Properties.Resources.live_folder_16px);
            imageList.Images.Add("subsubchild", Properties.Resources.reduce_10px);
            //Set Image List
            treeView1.ImageList = imageList;
        }
        private void comboLineno_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboLineno.SelectedIndex > -1)
            {
                int.TryParse(comboLineno.SelectedValue.ToString(), out int lineId);
                if (lineId == 0)
                    return;
                treeView1.Nodes.Clear();
                var data = JointBLL.LineJointsQcHistory(lineId);
                if (data != null)
                {
                    var _enumrableData = data.AsEnumerable();
                    var _kinds = from row in _enumrableData
                                 group row by row.Field<string>("Kind") into g
                                 select new
                                 {
                                     Kind = g.Key
                                 };
                    var _jointNo = from row in _enumrableData
                                   group row by row.Field<string>("JointNo") into g
                                   select new
                                   {
                                       JointNo = g.Key
                                   };
                    // Root node
                    TreeNode rootNode = new TreeNode(comboLineno.Text);
                    treeView1.Nodes.Add(rootNode);
                    rootNode.ImageIndex = 0;  // Parent node image index (ImageList index 0)
                    rootNode.SelectedImageIndex = 0;
                    //add joint no
                    foreach (var item in _jointNo)
                    {
                        AddNode(item.JointNo, comboLineno.Text, 1);
                        //add Report Kind
                        foreach (var kind in _kinds)
                        {
                            AddNode(kind.Kind, item.JointNo, 2);
                            //add Report No
                            var _child = _enumrableData.Where(r => r.Field<string>("JointNo") == item.JointNo && r.Field<string>("Kind") == kind.Kind);
                            
                            foreach (var report in _child)
                            {
                                AddNode(report.Field<string>("ReportNo"), kind.Kind, 3);
                            }
                        }
                    }
                }
            }
        }
        private void AddNode(string nodeText, string parent, byte imageIndex)
        {
            // Find the Level 3 node (for example, "Sub Child Node 1")
            TreeNode node = FindNode(parent);
            // Add a Level 4 node under "Sub Child Node 1"
            if (node != null)
            {
                TreeNode levelNode = new TreeNode(nodeText);
                node.Nodes.Add(levelNode);
                levelNode.ImageIndex = imageIndex;  // Child node image index (ImageList index 1)
                levelNode.SelectedImageIndex = imageIndex;  // Image when the node is selected
            }
        }
        private TreeNode FindNode(string nodeText)
        {
            foreach (TreeNode node in treeView1.Nodes)
            {
                TreeNode foundNode = FindNodeRecursive(node, nodeText);
                if (foundNode != null)
                {
                    return foundNode;
                }
            }
            return null; // Not found
        }
        private TreeNode FindNodeRecursive(TreeNode parentNode, string nodeText)
        {
            if (parentNode.Text == nodeText)
                return parentNode;
            foreach (TreeNode childNode in parentNode.Nodes)
            {
                TreeNode foundNode = FindNodeRecursive(childNode, nodeText);
                if (foundNode != null)
                    return foundNode;
            }
            return null; // Not found
        }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,098 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 47,581 Reputation points Microsoft Vendor
    2024-11-12T02:05:33.2966667+00:00

    Hi @Farshad Valizade , Welcome to Microsoft Q&A,

    The AddNode method is used to add nodes to the tree view, but its design causes the search to always start from the root node when adding child nodes, and stop when the first matching node is found. This will cause problems in a deep structure.

    Try this code and let me know if I understand it wrong:

    private void comboLineno_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboLineno.SelectedIndex > -1)
        {
            int.TryParse(comboLineno.SelectedValue.ToString(), out int lineId);
            if (lineId == 0)
                return;
    
            treeView1.Nodes.Clear();
            var data = JointBLL.LineJointsQcHistory(lineId);
            if (data != null)
            {
                var _enumrableData = data.AsEnumerable();
    
                TreeNode rootNode = new TreeNode(comboLineno.Text);
                treeView1.Nodes.Add(rootNode);
                rootNode.ImageIndex = 0;  
                rootNode.SelectedImageIndex = 0;
           
                foreach (var item in _enumrableData.Select(r => r.Field<string>("JointNo")).Distinct())
                {
                    TreeNode jointNode = new TreeNode(item);
                    rootNode.Nodes.Add(jointNode);
                    jointNode.ImageIndex = 1; 
                    jointNode.SelectedImageIndex = 1;
    
                    foreach (var kind in _enumrableData.Where(r => r.Field<string>("JointNo") == item).Select(r => r.Field<string>("Kind")).Distinct())
                    {
                        TreeNode kindNode = new TreeNode(kind);
                        jointNode.Nodes.Add(kindNode);
                        kindNode.ImageIndex = 2;  
                        kindNode.SelectedImageIndex = 2;
    
                        foreach (var report in _enumrableData.Where(r => r.Field<string>("JointNo") == item && r.Field<string>("Kind") == kind))
                        {
                            TreeNode reportNode = new TreeNode(report.Field<string>("ReportNo"));
                            kindNode.Nodes.Add(reportNode);
                            reportNode.ImageIndex = 3;  
                            reportNode.SelectedImageIndex = 3;
                        }
                    }
                }
                treeView1.ExpandAll();  
            }
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.