private TreeView BuildTree(TreeView tree,DataTable tb, string KeyField, string ParentKeyField, string NameField, long TopWithValue) ... { this.tvwDept.Nodes.Clear(); // 添加全部科室节点 TreeNode node=new TreeNode("全部科室",2,0); node.Tag=null; //AddNodes(node,tb,KeyField,ParentKeyField,NameField); tree.Nodes.Add(node); for(int i=0;i<=tb.Rows.Count-1;i++) ...{ if(Convert.ToInt64(tb.Rows[i][ParentKeyField])==TopWithValue) ...{ node=new TreeNode(tb.Rows[i][NameField].ToString().Trim()); node.Tag=tb.Rows[i][KeyField].ToString(); AddNodes(node,tb,KeyField,ParentKeyField,NameField); tree.Nodes.Add(node); } } return tree; } private void AddNodes(TreeNode parNode,DataTable tb, string KeyField, string ParentKeyField, string NameField) ... { DataTable tbChild=tb.Clone(); DataRow[] rows=tb.Select(ParentKeyField+"="+parNode.Tag.ToString()); if(rows.Length!=0) parNode.ImageIndex=1; else parNode.ImageIndex=2; for(int i=0;i<=rows.Length-1;i++) ...{ tbChild.Rows.Add(rows[i].ItemArray); } for(int i=0;i<=tbChild.Rows.Count-1;i++) ...{ TreeNode node=new TreeNode(tbChild.Rows[i][NameField].ToString().Trim()); node.Tag=tbChild.Rows[i][KeyField].ToString(); parNode.Nodes.Add(node); AddNodes(node,tb,KeyField,ParentKeyField,NameField); } }