节点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); } }
时间: 2024-10-13 19:03:42