namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
AddTree(0, (TreeNode)null);
}
public void AddTree(int ParentID, TreeNode pNode)
{ DataView dvTree = new DataView(northwindDataSet1.Tables[0]);
//过滤ParentID,得到当前的所有子节点
dvTree.RowFilter = "[PARENTID] = " + ParentID; foreach (DataRowView Row in dvTree)
{
if (pNode == null)
{ //'̀添加根节点
TreeNode Node = treeView1.Nodes.Add(Row["ConText"].ToString());
AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
}
else
{ //添加当前节点的子节点
TreeNode Node = pNode.Nodes.Add(Row["ConText"].ToString());
AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
}
}
}
}
}
不报错,但是不能创建节点,DataSet是用工具栏里面的数据工具建立的,类型化了。在这个地方循环时候DataRowView Row in dvTree,Row怎么每次都是空null?
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
AddTree(0, (TreeNode)null);
}
public void AddTree(int ParentID, TreeNode pNode)
{ DataView dvTree = new DataView(northwindDataSet1.Tables[0]);
//过滤ParentID,得到当前的所有子节点
dvTree.RowFilter = "[PARENTID] = " + ParentID; foreach (DataRowView Row in dvTree)
{
if (pNode == null)
{ //'̀添加根节点
TreeNode Node = treeView1.Nodes.Add(Row["ConText"].ToString());
AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
}
else
{ //添加当前节点的子节点
TreeNode Node = pNode.Nodes.Add(Row["ConText"].ToString());
AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
}
}
}
}
}
不报错,但是不能创建节点,DataSet是用工具栏里面的数据工具建立的,类型化了。在这个地方循环时候DataRowView Row in dvTree,Row怎么每次都是空null?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货