如何在treeview中利用数据动态生成树
2016-11-08
展开全部
//采用递归的方法动态建立Tree
private void CreateTree(TreeNodeCollection Nds, int parentID)
{
DataView dv = new DataView(dtMaster);
TreeNode tmpNd;
int intID;
dv.RowFilter = "parentid='" + parentID + "'";
foreach (DataRowView drv in dv)
{
intID = Convert.ToInt16(drv["nodeid"]);
tmpNd = new TreeNode();
tmpNd.Name = drv["nodeid"].ToString();
tmpNd.Text = drv["nodename"].ToString();
Nds.Add(tmpNd);
CreateTree(Nds[Nds.Count - 1].Nodes, intID);
}
}
private void frmNodeConfig_Load(object sender, EventArgs e)
{
FillDate();
this.trvMain.Nodes.Clear();
CreateTree(trvMain.Nodes, 0);
trvMain.Nodes[0].Expand();
}
private void CreateTree(TreeNodeCollection Nds, int parentID)
{
DataView dv = new DataView(dtMaster);
TreeNode tmpNd;
int intID;
dv.RowFilter = "parentid='" + parentID + "'";
foreach (DataRowView drv in dv)
{
intID = Convert.ToInt16(drv["nodeid"]);
tmpNd = new TreeNode();
tmpNd.Name = drv["nodeid"].ToString();
tmpNd.Text = drv["nodename"].ToString();
Nds.Add(tmpNd);
CreateTree(Nds[Nds.Count - 1].Nodes, intID);
}
}
private void frmNodeConfig_Load(object sender, EventArgs e)
{
FillDate();
this.trvMain.Nodes.Clear();
CreateTree(trvMain.Nodes, 0);
trvMain.Nodes[0].Expand();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询