public DataSet ds=null;
private void Load(object sender, System.EventArgs e)
{
// 定义数据库连接
SqlConnection CN = new SqlConnection();
try
{
//初始化连接字符串
CN.ConnectionString= "data source=pmserver;initial catalog=Benchmark;persist security info=False;user id=sa;Password=sa;";
CN.Open();
SqlDataAdapter adp = new SqlDataAdapter("select * from tbTree",CN);
ds=new DataSet();
adp.Fill(ds);
}
catch (Exception ex)
{
throw ex;
}
finally
{
CN.Close();
}
//调用递归函数,完成树形结构的生成
AddTree(0, (TreeNode)null);
}
// 递归添加树的节点
public void AddTree(int ParentID,TreeNode pNode)
{
DataView dvTree = new DataView(ds.Tables[0]);
//过滤ParentID,得到当前的所有子节点
dvTree.RowFilter = "[PARENTID] = " + ParentID;
foreach(DataRowView Row in dvTree)
{
TreeNode Node=new TreeNode() ;
if(pNode == null)
{ //'?添加根节点
Node.Text = Row["ConText"].ToString();
TreeView1.Nodes.Add(Node);
Node.Expanded=true;
AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
}
else
{ //添加当前节点的子节点
Node.Text = Row["ConText"].ToString();
pNode.Nodes.Add(Node);
Node.Expanded = true;
AddTree(Int32.Parse(Row["ID"].ToString()),Node); //再次递归
}
}
}
private void Load(object sender, System.EventArgs e)
{
// 定义数据库连接
SqlConnection CN = new SqlConnection();
try
{
//初始化连接字符串
CN.ConnectionString= "data source=pmserver;initial catalog=Benchmark;persist security info=False;user id=sa;Password=sa;";
CN.Open();
SqlDataAdapter adp = new SqlDataAdapter("select * from tbTree",CN);
ds=new DataSet();
adp.Fill(ds);
}
catch (Exception ex)
{
throw ex;
}
finally
{
CN.Close();
}
//调用递归函数,完成树形结构的生成
AddTree(0, (TreeNode)null);
}
// 递归添加树的节点
public void AddTree(int ParentID,TreeNode pNode)
{
DataView dvTree = new DataView(ds.Tables[0]);
//过滤ParentID,得到当前的所有子节点
dvTree.RowFilter = "[PARENTID] = " + ParentID;
foreach(DataRowView Row in dvTree)
{
TreeNode Node=new TreeNode() ;
if(pNode == null)
{ //'?添加根节点
Node.Text = Row["ConText"].ToString();
TreeView1.Nodes.Add(Node);
Node.Expanded=true;
AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
}
else
{ //添加当前节点的子节点
Node.Text = Row["ConText"].ToString();
pNode.Nodes.Add(Node);
Node.Expanded = true;
AddTree(Int32.Parse(Row["ID"].ToString()),Node); //再次递归
}
}
}