Populating TreeView with Data From Database Using Entity Framework

  • Thread starter Thread starter Godymn
  • Start date Start date
G

Godymn

Guest
Hello,

Please, i am still new to entity framework/LINQ and i have this challenge. I have also done some research to see if i could find any example related to mine, but couldn't.

I am working with treeview on Winforms and i want to load data from the database. I have a default root node already from the controls. And now, when i try adding a child node and fetching the data from the database, it creates two new root node and even the data in it is also wrong.

So far, this is the code i have which i have been trying for 2days now. Any help would be sincerely appreciated.

private void RefreshTree()
{
using (Practise2Entities db = new Practise2Entities())
{
var q = (from i in db.qm_tree
group i by i.created into grpDate
select grpDate);

foreach (var item in q)
{
TreeNode nodes = new TreeNode(q.ToString()); //Create root node first
TreeNode tn = item as TreeNode;

var childNodes = q;
foreach (var child in childNodes) //Adding child nodes to root node if it have any
{
TreeNode childNode = new TreeNode(q.ToString());
nodes.Nodes.Add(childNode);
}
DashBoard.fromDash.treeView1.Nodes.Add(nodes);
}
}

}

I want to have one root node(which is already default), then child nodes and sub nodes...etc

Please, how can i achieve this?

Continue reading...
 
Back
Top