日期:2014-05-18 浏览次数:20897 次
private void Form1_Load(object sender, EventArgs e) { SqlConnection sql = new SqlConnection(@"Data Source=LIUFENG\SQLEXPRESS;Initial Catalog=liufeng;Integrated Security=True"); SqlCommand cmd = new SqlCommand("Select * from TreeTable", sql); DataSet dss = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dss); DataTable dt = dss.Tables[0]; BindTree(gvTreeView, dt); } protected void BindTree(gvTreeView gvTreeView , DataTable dt) { gvTreeView.Nodes.Clear();//清空树节点 DataRow []row = dt.Select("ParentID is null"); if (row.Length <= 0) { return;//找不到根结点则返回 } gvTreeNode root = new gvTreeNode(); root.Text = row[0]["name"].ToString(); root.Tag = row[0]["ID"].ToString(); gvTreeView.Nodes.Add(root); CreateChildNode(root, dt); root.Expand(); } protected void CreateChildNode(gvTreeNode gvTreeNode, DataTable dt) { DataRow[] rows = dt.Select("ParentID ='" + gvTreeNode.Tag + "'"); foreach(DataRow row in rows) { gvTreeNode Node = new gvTreeNode(); Node.Text = row["name"].ToString(); Node.Tag = row["ID"].ToString(); gvTreeNode.Nodes.Add(Node); CreateChildNode(Node, dt); } }
------解决方案--------------------
treeView1.ExpandAll()
就可以展开啊。