Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
product
--RawProduct
---rawMaterial
----rawItem
--Raw Material
I want this type of tree view


my code is bellow
C#
conn.Open();
string q = "select * from View_BOMMDP";
DataTable union = new DataTable();
OdbcDataAdapter UnionQuery = new OdbcDataAdapter("select distinct ProductName as Node ,ProductDependency as ParentNode  from View_BOMMaster union select  RawMaterialName as Node,ProductNameDependancy as ParentNode from View_BOMDetails", conn);
OdbcCommand cmd = new OdbcCommand(q, conn);
DataTable dt = new DataTable();
UnionQuery.Fill(union);
union.AcceptChanges();

TreeNode node;
C#
OdbcDataReader dr = cmd.ExecuteReader();
for (int i = 0; i < union.Rows.Count; i++)
{
    if (union.Rows[i]["ParentNode"].ToString() == "")
    {

        node = new TreeNode(Convert.ToString(union.Rows[i]["Node"]));
        string pName = Convert.ToString(union.Rows[i]["Node"]);
        
        for (int i1 = 0; i1 < union.Rows.Count; i1++)
        {
            if (union.Rows[i1]["ParentNode"].ToString() ==pName)
            {
                node.Nodes.Add(Convert.ToString(union.Rows[i1]["Node"]));
                string cNode = Convert.ToString(union.Rows[i1]["Node"]);

                //for (int i11 = 0; i11 < union.Rows.Count; i11++)
                //{
                //    if (union.Rows[i11]["ParentNode"].ToString() == cNode)
                //    {
                //        node.Nodes.Add(Convert.ToString(union.Rows[i11]["Node"]));
                //        TreeViewProduct.Nodes.Add(cch);
                //    }
                //}

               
            }
        }
        TreeViewProduct.Nodes.Add(node);
    }
}
Posted
Updated 23-May-14 1:53am
v2

1 solution

C#
AddNodes(TreeViewProduct.Nodes, "", union); 


you need recursive method like below, not tested :-)

C#
private void AddNodes(TreeNodeCollection nodes, int parentNode, System.Data.DataTable dt)
{
    string filterExp = string.Format("ParentNode='{0}'", parentNode);
    foreach (System.Data.DataRow r in dt.Select(filterExp))
    {
        TreeNode item = new TreeNode()
        {
            Text = r[0].ToString(),
            Value = r[0].ToString()
        };
        this.AddNodes(item.ChildNodes, int.Parse(r[1].ToString()), dt);
        nodes.Add(item);
    }
}
 
Share this answer
 
Comments
Member 10271164 24-May-14 0:37am    
This code is not work properly they have not find item.ChildNode in the recursive Method

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