treeView递归计算总个数,求大神,在线等
数据库的大致结构是这样的
ID Name PID Qty
每个都有自己的Qty 也就是下面的UnSetPriceQty ,现在我要计算每个父类下面所有子集的总个数怎么做,帮忙改下下面的代码就是ChildUnSetPriceQty 的数量。
比如
根目录(20)
第一级目录(10)
第二级目录(5)
第二级目录(5)
第一级目录(10)
List<ProductCategoryTreeView> tvList = new List<ProductCategoryTreeView>();
if (e.Result != null)
{
foreach (ProductCategory productCategory in e.Result.Where(p => string.IsNullOrEmpty(p.ParentID)))
{
ProductCategoryTreeView treeViewInfo = new ProductCategoryTreeView();
treeViewInfo.ID = productCategory.ID;
treeViewInfo.Name = productCategory.Name;
treeViewInfo.ParentID = productCategory.ParentID;
treeViewInfo.ProductTypeID = productCategory.ProductTypeID;
treeViewInfo.UnSetPriceQty = productCategory.UnSetPriceQty;
//treeViewInfo.ChildUnSetPriceQty = e.Result.Where(x => x.ParentID == productCategory.ID).Sum(x => x.UnSetPriceQty);
this.AddProductCategoryItems(treeViewInfo, productCategory.ID, e.Result.ToList());
tvList.Add(treeViewInfo);
}
}
public void AddProductCategoryItems(ProductCategoryTreeView treeViewInfo, string pid, List<ProductCategory> productCategoryList)
{
List<ProductCategory> result = productCategoryList.Where(x => x.ParentID == pid).ToList();
foreach (ProductCategory item in result)
{
ProductCategoryTreeView tvInfo = new ProductCategoryTreeView();
tvInfo.ID = item.ID;
tvInfo.Name = item.Name;
tvInfo.ParentID = item.ParentID;
tvInfo.ProductTypeID = item.ProductTypeID;
tvInfo.UnSetPriceQty = item.UnSetPriceQty;
//tvInfo.ChildUnSetPriceQty = productCategoryList.Where(x => x.ParentID == item.ID).Sum(x => x.UnSetPriceQty);
//treeViewInfo.ChildUnSetPriceQty+=tvInfo.ChildUnSetPriceQty;
treeViewInfo.Childrens.Add(tvInfo);
AddProductCategoryItems(tvInfo, item.ID, productCategoryList);
}
}
[解决办法]
用递归做吧
public static void getcheckednode(treenodecollection tnc)
{
foreach(treenode node in tnc)
{
if(node.checked)
{
messagebox.show(node.text);
}
getcheckednode(node.nodes);
}
}