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:
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.
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());
}