简体   繁体   中英

Fill Tree View dynamically from SQL Database using C#

Please help me to populate a tree view from SQL database dynamically. I am very new to C# and step by step processes are welcome. I have two table formats given in the picture below and I want to fill the tree view from database accordingly. I searched on other resource sites also but didn't find the solution what I am looking for.

Please Help !!

Please view given image,

enter image description here

I suppose you have two choices

You either run a JOIN which will create a result from the db where the department name is repeated over and over, and the sub department name is unique; you then loop over this result set row by row with a logic of:

  • try and retrieve the tree node with the department name X
  • if it failed because there was no tree node X add a new tree node X to the children of the root node
  • now there certainly is a node for department X, either new or already existing: add the child sub department to the node

Or, you run two loops

  • Query the departments and start a loop over the results
  • create a node for the current department row
  • query the database for the sub departments of this current department
  • start another loop that adds the sub departments just queries as children of the current department node

In either case you'll need to query a db, so get that part working. Entity Framework will help if you want to use it

Also, look into adding nodes to a tree view and get familiar with it - don't need a db for this, just code it up by hand manually adding nodes for practice

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM