C# TreeView,递归循环数据加载到treeView1中
标签:des style blog io color ar for sp 数据
TblAreaBLL bll = new TblAreaBLL();
private void button1_Click(object sender, EventArgs e)
{
LoadDataToTree(this.treeView1.Nodes, bll.GetTblAreaListByID(0));
}
private void LoadDataToTree(TreeNodeCollection treeNode, List list)
{
foreach (TblArea arr in list)
{
TreeNode tree = treeNode.Add(arr.AreaName);
tree.Tag = arr.AreaId;
LoadDataToTree(tree.Nodes, bll.GetTblAreaListByID(arr.AreaId)); //递归循环
}
}
private void button2_Click(object sender, EventArgs e)
{
List list = bll.GetTblAreaList(); //获取所有的城市升级列表
LoadDataToTree2(this.treeView1.Nodes, list, 0); //先加载所有省级列表
}
private void LoadDataToTree2(TreeNodeCollection treeNode, List list, int pid)
{
foreach (TblArea arr in list)
{
if (arr.AreaPId == pid)
{
TreeNode tree = treeNode.Add(arr.AreaName);
tree.Tag = arr.AreaId;
LoadDataToTree2(tree.Nodes, list, arr.AreaId); //递归循环 直接在集合中查询。
}
}
}
//删除节点下的所有数据
private void button3_Click(object sender, EventArgs e)
{
TreeNode node = this.treeView1.SelectedNode;
if (node != null)
{
int areaId = (int)node.Tag;
bll.DeleteTblAreaDiGui(areaId);
this.treeView1.SelectedNode.Remove();
MessageBox.Show("删除成功!");
}
}
C# TreeView,递归循环数据加载到treeView1中
标签:des style blog io color ar for sp 数据
原文地址:http://www.cnblogs.com/han1982/p/4060440.html
评论