码迷,mamicode.com
首页 > 其他好文 > 详细

递归获取子节点

时间:2015-08-26 13:58:07      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

节点ID,父节点ID,根据节点获取该节点下所有子节点。用于点击类别查询此类别下所有商品

        private string GetChildIdsById(int id)
        {
            List<ProductCategoryModel> listCategory = GetDataCategory();
            string ids = id.ToString();
             GetChilds(id, listCategory,ref ids);
             return ids;
        }
        private void GetChilds(int id, List<ProductCategoryModel> listc,ref string ids)
        {
           
            foreach (var item in listc.Where(p => Convert.ToInt32(p.FId) == id).ToList())
            {
                ids += "," + item.Id;
                GetChilds(Convert.ToInt32(item.Id), listc,ref ids);
            }
            
        }

 递归绑定目录(Winform)

        private void BindTreeView()
        {
            try
            {
                treeView1.BeginUpdate();
                treeView1.Nodes.Clear();
                List<ProductCategoryModel> listCategory = GetDataCategory();
                if (listCategory.Count > 0)
                {
                    //绑定根目录
                    ProductCategoryModel model = listCategory.Where(a => a.FId == -1).FirstOrDefault();
                    if (model != null)
                    {
                        TreeNode rootNode = new TreeNode();
                        rootNode.Text = model.Name;
                        rootNode.Tag = model.Id.ToString();
                        rootNode.ImageIndex = 0;
                        treeView1.Nodes.Add(rootNode);
                        AddNodes(rootNode, listCategory);
                    }
                    if (treeView1.Nodes.Count > 0)
                    {
                        treeView1.ExpandAll();
                    }
                }
                treeView1.EndUpdate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
            }

        }
        private static void AddNodes(TreeNode pNode, List<ProductCategoryModel> listc)
        {
            pNode.Nodes.Clear();
            foreach (var item in listc.Where(p => p.FId == Convert.ToInt32(pNode.Tag)).ToList())
            {
                TreeNode parentNode = new TreeNode();
                parentNode.Text = item.Name;
                parentNode.Tag = item.Id;
                parentNode.ImageIndex = 1;
                AddNodes(parentNode, listc);
                pNode.Nodes.Add(parentNode);
            }
        }

  

 

递归获取子节点

标签:

原文地址:http://www.cnblogs.com/huangzhen22/p/4760082.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!