我现在要做一个treeview动态绑定数据库表中的内容,表结构如下:
s_kmbm(科目编码) i_wdid(网点) i_grade(科目级别) s_bm1(编码1) s_bm2(编码2) s_bm3 s_bm4 s_bm5
101 0 1 101 null null null null
10101 1 2 101 01 null null null
102 1 1 102 null null null null
10201 1 1 102 01 null null null
其中i_grade是用来判别节点类型的.
请问各位高手如何实现绑定treeview的功能,实现树形目录树.
------解决方案--------------------
这个我试过了,可以了
page_load和一个函数就够了
累死了...
- C# code
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = xxxx; conn.Open(); SqlDataAdapter sda = new SqlDataAdapter("select s_kmbm,i_grade,s_bm1,s_bm2,s_bm3,s_bm4,s_bm5 from Table1 order by i_grade desc", conn); DataTable dt = new DataTable(); try { sda.Fill(dt); sda.Dispose(); conn.Close(); conn.Dispose(); } catch { sda.Dispose(); conn.Close(); conn.Dispose(); conn = null; sda = null; } TreeNode tr = new TreeNode("根节点", "0"); AddNode(tr, dt); this.TreeView1.Nodes.Clear(); this.TreeView1.Nodes.Add(tr); } } private void AddNode(TreeNode tn, DataTable dt) { for (int i = 0; i < dt.Rows.Count; i++) { int grade = int.Parse(tn.Value) + 1; if (dt.Rows[i].ItemArray[1].ToString() == "1" && tn.Value == "0") { TreeNode subtn = new TreeNode(dt.Rows[i].ItemArray[0].ToString(), dt.Rows[i].ItemArray[1].ToString()); AddNode(subtn, dt); tn.ChildNodes.Add(subtn); } else if ((int.Parse(dt.Rows[i].ItemArray[1].ToString()) - grade) == 0) { string pNode = ""; for (int k = 2; k < grade + 1; k++) { pNode += dt.Rows[i].ItemArray[k].ToString(); } if (pNode == tn.Text) { TreeNode subtn = new TreeNode(dt.Rows[i].ItemArray[0].ToString(), dt.Rows[i].ItemArray[1].ToString()); AddNode(subtn, dt); tn.ChildNodes.Add(subtn); } } } }