日期:2014-05-18 浏览次数:20758 次
private void bindTreeView1() { string sql = "select * from TableName"; DataTable dt = db.ExecuteDataTable(sql, CommandType.Text, null); DataRow[] dr = dt.Select("last_parent=0"); for (int i = 0; i < dr.Length; i++) { TreeNode tn = new TreeNode(); tn.Text = dr[i]["last_text"].ToString(); tn.Tag = dr[i]["last_id"].ToString(); FillTree(tn, dt); treeView1.Nodes.Add(tn); } } private void FillTree(TreeNode node, DataTable dt) { DataRow[] drr = dt.Select("last_parent='" + node.Tag.ToString() + "'"); if (drr.Length > 0) { for (int i = 0; i < drr.Length; i++) { TreeNode tnn = new TreeNode(); tnn.Text = drr[i]["last_text"].ToString(); tnn.Tag = drr[i]["last_id"].ToString(); if (drr[i]["last_parent"].ToString() == node.Tag.ToString()) { FillTree(tnn, dt); } node.Nodes.Add(tnn); } } }