1、 (数据库设计如上)
2、代码:
private void SetArea()
{ DataTable dt = new DataTable(); dt = customer.GetArea(); DataView dv = dt.DefaultView; DataTable dtProvince = dv.ToTable(true, "province", "provinceid"); for (int i = 0; i < dtProvince.Rows.Count; i++) { TreeNode nd = new TreeNode(); nd.Tag = dtProvince.Rows[i]["provinceid"].ToString(); nd.Text = dtProvince.Rows[i]["province"].ToString(); TreeArea.Nodes.Add(nd); //插入子节点 CreateTreeNode(ref nd, dt); TreeArea.ExpandAll(); TreeArea.CheckBoxes = true; } } private void CreateTreeNode(ref TreeNode tnParent, DataTable dt) { DataRow[] drArr = dt.Select("provinceid = '" + tnParent.Tag + "'"); if (drArr.Length == 0) return;for (int i = 0; i < drArr.Length; i++)
{ DataRow row = drArr[i]; TreeNode nd = new TreeNode(); nd.Tag = row["purchaseid"].ToString(); nd.Text = row["areaname"].ToString(); tnParent.Nodes.Add(nd); CreateTreeNode(ref nd, dt); } }3、运行结果: