private void DiGuiFillTreeView(int ParentCode,TreeNode pNode)//ParentCode为此菜单的上级编码
{
DataSet ds = GetData(ParentCode);
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TreeNode node = new TreeNode();
node.Text = ds.Tables[0].Rows[i]["cdmc"].ToString();
pNode.Nodes.Add(node);
DiGuiFillTreeView(Convert.ToInt32(ds.Tables[0].Rows[i]["xh"]), node);
}
}
}
private void Form3_Load(object sender, EventArgs e)
{
TreeNode rootnode = new TreeNode();
rootnode.Text = "管理";
treeView1.Nodes.Add(rootnode);
DiGuiFillTreeView(0, rootnode);
}
private DataSet GetData(int parentid)
{
string SQL = string.Format(@"select xh,sjbm,cdmc from cdxxb where sjbm={0}", parentid);
SqlConnection conn = new SqlConnection("server=.;database=EIMS;uid=sa;pwd=zd;");
conn.Open();
SqlDataAdapter da= new SqlDataAdapter(SQL,conn);
conn.Close();
DataSet ds= new DataSet();
da.Fill(ds);
return ds;
}
转载于:https://blog.51cto.com/andysu/957392