日期:2014-05-18  浏览次数:20774 次

c#动态添加access数据库数据到treeview winfrom
private void BindTree()
  {
  TreeNode root = new TreeNode();
  root.Text = "自定义疾病";
  DataTable dt = new DataTable();
  DataSet ds = GetTreeViewNode1(0);
  dt = ds.Tables[0];
  if (dt.Rows.Count > 0)
  {
  foreach (DataRow dr in dt.Rows)
  {
  TreeNode node = new TreeNode();
  node.Text = dr["description"].ToString();
  int PID = Convert.ToInt32(dr["aid"]);
  DataTable dt1 = new DataTable();
  DataSet ds1 = GetTreeviewNodeBySid(PID);
  dt1 = ds1.Tables[0];
  // dt1 = GetTreeviewNodeBySid(PID);
  if (dt1.Rows.Count > 0)
  {
  foreach (DataRow dr1 in dt1.Rows)
  {
  int aid1 = Convert.ToInt32(dr1["aid"]);
  node.Nodes.Add(CreateTreeNode(aid1));
  }
  }
  root.Nodes.Add(node);
  }
  treeView1.Nodes.Add(root);
  }
  }
  private TreeNode CreateTreeNode(int Aid)
  {
  TreeNode node2 = new TreeNode();
  DataTable dt1 = new DataTable();
  DataSet ds1 = GetTreeviewNodeBySid(Aid);
  dt1 = ds1.Tables[0];
  //dt1 = GetTreeviewNodeBySid(Aid);
  foreach (DataRow dr in dt1.Rows)
  {
  node2.Text = dr["description"].ToString();
  int aID = Convert.ToInt32(dr["aid"]);

  DataTable dt = new DataTable();
  DataSet ds = GetTreeviewNodeBySid(aID);
  dt = ds1.Tables[0];
  //dt = GetTreeviewNodeBySid(aID);
  if (dt.Rows.Count > 0)
  {
  foreach (DataRow dr3 in dt1.Rows)
  {
  int fID2 = Convert.ToInt32(dr3["aid"]);
  node2.Nodes.Add(CreateTreeNode(fID2));
  }
  }
  }
  return node2;
  }
  public DataSet GetTreeViewNode1(int num)
  {
  string sql2 = "select * from SubAnotomical where aid= '" + num + "'";
  DataSet ds = AccessFactory.getDataSet(sql2);
  return ds;
  }


  public DataSet GetTreeviewNodeBySid(int num)
  {
  string sql2 = "select * from SubAnotomical where sid= '" + num + "'";
  DataSet ds2 = AccessFactory.getDataSet(sql2);
  return ds2;
  }


------解决方案--------------------
参考http://www.cnblogs.com/duhongyu216/articles/1702591.html