0

Hi have a data Table with 3 fields and my expected tree view will like this below image.

Expected Result

My data table returns the details like this.

Data table result

And i tried the below code. Here child node not getting listing properly

 public void BuildTree(DataTable dt, TreeView trv, Boolean expandAll)
    {
        trv.Nodes.Clear();
        TreeNode node = default(TreeNode);
        TreeNode subNode = default(TreeNode);
        foreach (DataRow row in dt.Rows)
        {
            node = Searchnode(row[0].ToString(), trv);
            if (node != null)
            {
                subNode = new TreeNode(row[1].ToString());
                node.Nodes.Add(subNode);
            }
            else
            {
                node = new TreeNode(row[0].ToString());
                subNode = new TreeNode(row[1].ToString());
                node.Nodes.Add(subNode);
                trv.Nodes.Add(node);
            }
        }
        if (expandAll)
        {
            trv.ExpandAll();
        }
    }
    private TreeNode Searchnode(string nodetext, TreeView trv)
    {
        foreach (TreeNode node in trv.Nodes)
        {
            if (node.Text == nodetext)
            {
                return node;
            }
        }
        return null;
    }
2
  • Here child node not getting listing properly What do you want the code to do? What does it actually do? Commented Sep 7, 2017 at 12:46
  • I want to populate the tree view like in the image based on the data table i have. But i am not able to add second child Commented Sep 7, 2017 at 12:59

1 Answer 1

1

I'm suposing that datatable is previosly ordered by CustomerName, DeliverySchedule, Name

Initialize test data:

    private void InitializeDataTable() {
        dt = new DataTable();
        dt.Columns.Add("DeliverySchedule");
        dt.Columns.Add("Name");
        dt.Columns.Add("CustomerName");

        AddRow("Daily", "Test", "Team Venkat");
        AddRow("Daily", "TestB", "Team Venkat");
        AddRow("Weekly", "OtherName", "Team Venkat");
        AddRow("Weekly", "OtherName2", "Team Venkat");
        AddRow("Daily", "Test", "Team2");
        AddRow("Weekly", "Test", "Team2");

    }

    private void AddRow(string schedule, string name, string customer) {
        DataRow row = dt.NewRow();
        row[0] = schedule;
        row[1] = name;
        row[2] = customer;
        dt.Rows.Add(row);
    }

Load tree from DataTable using a three level loop:

    private void LoadBtn_Click(object sender, EventArgs e) {
        int i = 0;
        treeView1.Nodes.Clear();
        while (i < dt.Rows.Count) {
            DataRow row = dt.Rows[i];
            string customer = row.Field<string>(2);
            TreeNode customerNode = treeView1.Nodes.Add(customer);
            while (i < dt.Rows.Count && row.Field<string>(2) == customer) {
                string schedule = row.Field<string>(0);
                TreeNode scheduleNode = customerNode.Nodes.Add(schedule);
                while (i < dt.Rows.Count && row.Field<string>(2) == customer && schedule == row.Field<string>(0)) {

                    string report = row.Field<string>(1);
                    scheduleNode.Nodes.Add(report);
                    if (++i < dt.Rows.Count)
                        row = dt.Rows[i];
                }
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.