日期:2014-05-20 浏览次数:20831 次
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); } } } }