日期:2014-05-18 浏览次数:20856 次
public static void treeView增加内容(string id, string name, string PName,string table, TreeView tv) { using (SqlConnection cn = Conn.connMsSql()) { cn.Open(); SqlCommand cmd = cn.CreateCommand(); cmd.CommandText = "select "+id+" ,"+name+" ,"+PName+" from "+table; // cmd.Parameters.Add( new SqlParameter( "@id",id )); // cmd.Parameters.Add(new SqlParameter("@name",name)); // cmd.Parameters.Add(new SqlParameter("@PName", PName)); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds);//得到DataSet AddTree(null, (TreeNode)null,PName,ds,tv); } } static void AddTree(string ParentID, TreeNode pNode, string PName, DataSet ds, TreeView tv) { TreeNode tn1 = new TreeNode(); DataView dvTree = new DataView(ds.Tables[0]); dvTree.RowFilter =(PName=ParentID); foreach (DataRowView Row in dvTree) { if (pNode == null) { tn1.Text = Row[1].ToString();//row[1]表示name; tv.Nodes.Add(tn1); string id = Row[0].ToString(); AddTree(id, tn1, PName, ds, tv); } else { TreeNode tn2 = new TreeNode(); tn2.Text = Row[1].ToString(); pNode.Nodes.Add(tn2); string id = Row[0].ToString(); AddTree(id, tn2, PName, ds, tv); //再次递归 } } }