المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : populating a treeview?



C# Programming
06-10-2010, 09:51 PM
I'm trying to build a treeview of 4 layers, using 4 tables in the database.

Layer 1: Departments
Layer 2: Groups
Layer 3: Products
Layer 4: Barcodes

No I've had a go at getting the 4 tables in as a dataset, and tried to populate the treeview, but only the first 2 layers will show up in my tree view.

If I make layer 2 my first layer, only layer 2 and 3 will show up.

private void Load_tree()
{
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CCS_HOConnectionString"].ConnectionString);
SqlDataAdapter daDepts = new SqlDataAdapter("barcodePrinter_get_Departments", conn);
SqlDataAdapter daGroups = new SqlDataAdapter("barcodePrinter_get_Groups", conn);
SqlDataAdapter daProducts = new SqlDataAdapter("barcodePrinter_get_Products", conn);
SqlDataAdapter daBarcode = new SqlDataAdapter("barcodePrinter_get_Barcode", conn);


daDepts.Fill(ds, "DEPTS");
daGroups.Fill(ds, "PRODGRP");
daProducts.Fill(ds, "PRODUCT");
daBarcode.Fill(ds, "product_barcode");


ds.Relations.Add("Depts_Group", ds.Tables["DEPTS"].Columns["DEPTCODE"], ds.Tables["PRODGRP"].Columns["PGRPDEPT"]);
ds.Relations.Add("Group_Product", ds.Tables["PRODGRP"].Columns["PGRPID"], ds.Tables["PRODUCT"].Columns["PRODGRP"]);
ds.Relations.Add("ProdBarcode", ds.Tables["PRODUCT"].Columns["PRODCODE"], ds.Tables["product_barcode"].Columns["prodcode"]);

dataGridView1.DataSource = ds.Tables["DEPTS"];
dataGridView2.DataSource = ds.Tables["PRODGRP"];
dataGridView3.DataSource = ds.Tables["PRODUCT"];
dataGridView4.DataSource = ds.Tables["product_barcode"];

foreach (DataRow dr in ds.Tables["DEPTS"].Rows)
{
TreeNode tn = new TreeNode(dr["DEPTSHORT"].ToString());
foreach (DataRow drGroup in dr.GetChildRows("Depts_Group"))
{
TreeNode tnn = new TreeNode(drGroup["PGRPSHORT"].ToString());
foreach (DataRow drProduct in dr.GetChildRows("Group_Product"))
{
TreeNode tnnn = new TreeNode(drProduct["PRODSHORT"].ToString());
foreach (DataRow drBarcode in dr.GetChildRows("Product_Barcode"))
{
tnnn.Nodes.Add(drBarcode["barcode"].ToString());
}
tnn.Nodes.Add(tnnn);
}
tn.Nodes.Add(tnn);
}
trvBarcodes.Nodes.Add(tn);
}
trvBarcodes.ExpandAll();
}

I'm looking for a solution that will make all 4 layers show in the treeview.
Like
+Department 1
-+Group 1
--+Product 1
----Barcode 1
----Barcode 2
-+Group 2
--+Product 2
----Barcode 3
--+Product 3
----Barcode 4

I never know how many layers there will be within the child node, this is decided by the list in the database and that one can change.