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
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");
foreach(DataRow dat1 in ds.Tables["TabelParent"].Rows)
{
TreeNode tn1 = new TreeNode(dat1["Parent"].ToString());
foreach (DataRow dat2 in ds.Tables["TabelCategory"].Rows)
{
TreeNode tn2 = new TreeNode(dat2["Category"].ToString());
if (dat1["ID"].ToString() == dat2["ParentID"].ToString())
{
foreach (DataRow dat3 in ds.Tables["TabelType"].Rows)
{
TreeNode tn3 = new TreeNode(dat3["Type"].ToString());
if (dat2["ID"].ToString() == dat3["CatID"].ToString())
{
foreach(DataRow dat4 in ds.Tables["TabelSubType"].Rows)
{
TreeNode tn4 = new TreeNode(dat4["SubType"].ToString());
if (dat3["ID"].ToString() == dat4["TypeID"].ToString())
{
foreach(DataRow dat6 in ds.Tables["TabelModel"].Rows)
{
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);
}
}
}
}
tn3.Nodes.Add(tn4);
}
}
tn2.Nodes.Add(tn3);
}
}
tn1.Nodes.Add(tn2);
}
}
treeView1.Nodes.Add(tn1);
}
}
}
i can generate until Sub,, but the system and so on was a mess..
So if anyone can help, i'd be thankfull..