0

I want to populate hierarchical Tree Structure using recursive data from the database using button_click event

I have the following record in the table.

bomItem          partId
500101            100200
500101            500100
500101            100280
500100            320255

I want to show Tree as shown below
   500101
     +----100200
     +----500100
           +----300255
     +----100280

// Note: bomItem is the parent and partId is the child fields

The problem i am having:

  1. when navigate to the next record keeps tree structure from the previous record, i want to clear Tree-view when i navigate to another record.

  2. To show the full tree structure i have to populate the child's first

Here is the code i used

//button click event

private void button4_Click(object sender, EventArgs e)
        {

            _command = new SqlCommand("SELECT * FROM [BOMDETAIL] where bomItem= '" + int.Parse(bomItemTextBox.Text) + "' and bomRev= '" + bomRevComboBox.Text + "'", _connection);
            _adapter = new SqlDataAdapter(_command);
            _adapter.Fill(objdataset);
            _connection.Close(); 

            try
            {
                TreeNode node = new TreeNode(bomItemTextBox.Text);

                filltree(node, int.Parse(bomItemTextBox.Text), objdataset);

                treeView2.Nodes.Add(node);

                //dataGridView1.DataSource = objdataset.Tables[0];
            }
            catch (Exception c)
            {

                MessageBox.Show(c.ToString());
            }
        }

//filetree()

 private void filltree(TreeNode parentnode, int parentid, DataSet dataset)

        {

            // Fill Tree Function to poplulate treeeview control

            String str = null;
            try
            {


                foreach (DataRow row in dataset.Tables[0].Rows)
                {
                    if (Convert.ToInt32(row["bomItem"]) == parentid)
                    {
                        str = row["partId"].ToString();
                        TreeNode new_parent_node = parentnode.Nodes.Add(str);
                        int parent_id = Convert.ToInt32(row["partId"]);
                        filltree(new_parent_node, parent_id, dataset);
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

Image describing problem No 1

afri
  • 49
  • 2
  • 12
  • I'm confused by "when navigate to the next record keeps tree structure from the previous record, i want to clear Tree-view when i navigate to another record." Are you saying that you want the user to expand a node and this refreshes the whole tree? If this is the case then that would likely be very confusing to the end user. – rune711 Nov 30 '16 at 20:18
  • @rune711 let say i have two record `500100` and `500101`. while i am on `500100` i clicked `button4` and that will show me the `partId` value of this record in `Treeview1` and i clicked `Next` button to navigate to the next record and lets say the next record is `500101` when i click `button4` again that give me both treevalue of 500100`` and `500101`. does that make sense? – afri Nov 30 '16 at 20:26
  • @rune711 i added image of the treeView at the bottom of my question. that might help – afri Nov 30 '16 at 20:31

0 Answers0