Click here to Skip to main content
15,888,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Guys, i really need help..
I want to populate a treeview that has 6 level and data was taken from database.

Wanted Treeview
- Parent
- Category
- Type
- Sub-Type
- System
- Model

Table 1 : TabelParent
| ID | Parent |

Table 2 : TabelCategory
| ID | Category | ParID | //ParID -> Foreign key

Table 3 : TabelType
| ID | Type | CatID |

confused with table 4 and 5

Table 6 : TabelModel
| ParID | CatID | TypeID | SubID | SysID | ModelID | Model_Name |


Every Model has Parent, category and Type,,
and the problem is, Not every Model has SubType or System (can be both)..
so it can be Complete or no Sub-Type or no System or no SubType and no System..

i have tried to make it like this
C#
public partial class Form3 : Form
    {
        SqlConnection cn = new SqlConnection(@"Data Source=SAMMY-PC\SQLEXPRESS;Initial Catalog=Prod_Knowledge Nayati;Integrated Security=True");
        public Form3()
        {
            InitializeComponent();
            tree();
        }

        private void tree()
        {
            treeView1.Nodes.Clear();
            cn.Open();
            SqlDataAdapter Par = new SqlDataAdapter("Select * FROM TabelParent", cn);
            SqlDataAdapter Cat = new SqlDataAdapter("SELECT * FROM TabelCategory", cn);
            SqlDataAdapter Type = new SqlDataAdapter("SELECT * FROM TabelType", cn);
            SqlDataAdapter Sub = new SqlDataAdapter("SELECT * FROM TabelSubType", cn);
            SqlDataAdapter Sys = new SqlDataAdapter("SELECT * FROM TabelSystem", cn);
            SqlDataAdapter Mod = new SqlDataAdapter("SELECT * FROM TabelModel", cn);
            DataSet ds = new DataSet();
            Par.Fill(ds, "TabelParent");
            Cat.Fill(ds, "TabelCategory");
            Type.Fill(ds, "TabelType");
            Sub.Fill(ds, "TabelSubType");
            Sys.Fill(ds, "TabelSystem");
            Mod.Fill(ds, "TabelModel");
            //DataColumn dc1 = ds.Tables["TabelCategory"].Columns["Index"];

            //DataColumn dc2 = ds.Tables["TabelModel"].Columns["Index1"];
            //DataColumn dc3 = ds.Tables["TabelType"].Columns["Index"];
            //DataColumn dc4 = ds.Tables["TabelModel"].Columns["Index2"];

            //DataRelation dr1 = new DataRelation("CatIndex", dc1, dc2);
            //DataRelation dr2 = new DataRelation("TypeIndex", dc3, dc4);

            //ds.Relations.Add(dr1);
            //ds.Relations.Add(dr2);

            foreach(DataRow dat1 in ds.Tables["TabelParent"].Rows)
            {//1 Parent
                TreeNode tn1 = new TreeNode(dat1["Parent"].ToString());
                foreach (DataRow dat2 in ds.Tables["TabelCategory"].Rows)
                {//2 Category
                    TreeNode tn2 = new TreeNode(dat2["Category"].ToString());
                    if (dat1["ID"].ToString() == dat2["ParentID"].ToString())
                    {// if P.Parent = F.Par - Cat
                        foreach (DataRow dat3 in ds.Tables["TabelType"].Rows)
                        {//3 Type
                            TreeNode tn3 = new TreeNode(dat3["Type"].ToString());
                            if (dat2["ID"].ToString() == dat3["CatID"].ToString())
                            {// if P.Cat = F.Cat - Type
                                foreach(DataRow dat4 in ds.Tables["TabelSubType"].Rows)
                                {//4 SubType
                                    TreeNode tn4 = new TreeNode(dat4["SubType"].ToString());
                                    if (dat3["ID"].ToString() == dat4["TypeID"].ToString())
                                    {
                                        // THE MESS STARTED HERE
                                        foreach(DataRow dat6 in ds.Tables["TabelModel"].Rows)
                                        {//5 Model
                                            TreeNode tn6 = new TreeNode(dat6["Model"].ToString());
                                            if (dat6["SubID"].ToString() == "0")
                                            {
                                                TreeNode tn5; 
                                                foreach (DataRow dat5 in ds.Tables["TabelSystem"].Rows)
                                                {
                                                    
                                                    if (dat5["ID"].ToString() == dat6["SysID"].ToString())
                                                    {
                                                        tn5 = new TreeNode(dat5["System"].ToString());
                                                        tn3.Nodes.Add(tn5);
                                                        tn5.Nodes.Add(tn6);
                                                    }
                                                }
                                                
                                            }
                                            //tn4.Nodes.Add(tn6);


                                        }//5 Model
                                        tn3.Nodes.Add(tn4);
                                    }


                                    
                                }//4 SubType

                                tn2.Nodes.Add(tn3);
                            }// if Type
                        }//3 Type
                        tn1.Nodes.Add(tn2);
                    }// if cat
                }//2 Category
                treeView1.Nodes.Add(tn1);
            }//1 Parent
        }
    }


i can generate until Sub,, but the system and so on was a mess..
So if anyone can help, i'd be thankfull..
Posted
Updated 18-Jun-13 18:53pm
v2

1 solution

Yes, all of this is a disaster. If you're doing this for someone, they will notice that you spelled table, 'tabel', but, why preface each table name with it any how ?

I would do it completely differently. I would read the data for the top level, then have a method that inserts each top level item, and then inside that method, run SQL to find sub items for that level. then a method is called to insert each new level, which does the same thing again. Or I'd write one SQL statement that returned all the data I needed, grouped by parent node details so I could just iterate over it to build my data.
 
Share this answer
 
Comments
Sam Oryza Reyno 19-Jun-13 0:42am    
i called it as 'tabel' because i'm Indonesian..
I made 6 table for each level,, and the Model Table with 5 Foreign key..

I was blank right now,, so if u could give me some code, i'd really helped..
Before Thanks
Sam Oryza Reyno 19-Jun-13 0:56am    
I add my table design,, so if u have any suggestion to change it, please tell me
Christian Graus 19-Jun-13 1:46am    
You only need to store the id of the direct parent. The other data is superfluous and useless.
Sam Oryza Reyno 19-Jun-13 22:36pm    
can u help me with some code? coz im losing my mind because of this..
Christian Graus 19-Jun-13 23:44pm    
Why do you need to do this right now ? If it's paid work, it's clearly beyond you. If it's for a class, you should talk to your teacher. If it's for your own use, you should probably consider paying someone to do it for you. I've explained everything that you need to do. If you were to try to do what I said and ask specific questions, I would help, but I won't spend an hour doing your job for you, sorry.

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