Reputation: 413
I'm Loading a TreeView from database with Linq, and my code only add the last parent item but all the category item under that parent item.
My stored prcedures from Linq works fine, i use them else where.
Can someone help me find my error here?
Thanks
LinqtoDBDataContext db = new LinqtoDBDataContext();
var parents = db.GetParentCategories();
TreeNode parentNode = null;
foreach (var parent in parents)
{
parentNode = new TreeNode(parent.ParentCatName.ToString());
var categories = db.GetCategories(parent.ParentCatID);
foreach (var category in categories)
{
TreeNode childNode = new TreeNode(category.CategoryName.ToString());
parentNode.ChildNodes.Add(childNode);
var products = db.GetProductsInCategory(category.CategoryID);
foreach (var product in products)
{
TreeNode child3Node = new TreeNode(product.ProductName.ToString());
childNode.ChildNodes.Add(child3Node);
}
}
}
TreeViewProducts.Nodes.Add(parentNode);
Upvotes: 0
Views: 503
Reputation: 4797
You are resetting your parentNode for every parent. Move TreeViewProducts.Nodes.Add(parentNode);
into your foreach
loop.
Upvotes: 1