简体   繁体   English

使用 C# 从 SQL 数据库动态填充树视图

[英]Fill Tree View dynamically from SQL Database using C#

Please help me to populate a tree view from SQL database dynamically.请帮助我从 SQL 数据库动态填充树视图。 I am very new to C# and step by step processes are welcome.我对 C# 非常陌生,欢迎逐步进行。 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;您可以运行一个 JOIN ,它将从数据库中创建一个结果,其中部门名称一遍又一遍地重复,并且子部门名称是唯一的; 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尝试检索部门名称为 X 的树节点
  • if it failed because there was no tree node X add a new tree node X to the children of the root node如果因为没有树节点 X 而失败,则将新的树节点 X 添加到根节点的子节点
  • now there certainly is a node for department X, either new or already existing: add the child sub department to the node现在肯定有一个部门 X 的节点,无论是新的还是已经存在的:将子部门添加到节点

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此外,考虑将节点添加到树视图并熟悉它 - 不需要数据库,只需手动编写代码手动添加节点以进行练习

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Winforms应用程序中使用C#将数据库数据绑定到树形视图 - Binding the database data to tree view using c# in winforms applications 使用C#动态将所有表的数据从一个SQL Server数据库插入第二个数据库 - Insert data from one SQL Server database into a second database for all tables dynamically using C# 如何使用asp.net C#中的Ajax和Web服务使用数据库中的数据填充动态创建的下拉列表 - how fill the dynamically created dropdownlist with data from database using ajax and web service in asp.net c# 如何使用c#从文件系统和SQL Server中删除动态生成的数据库文件(.MDF和.LDF) - How to delete a dynamically generated database file (.MDF and .LDF) from the filesystem and from SQL server using c# 从C#动态更改椭圆填充 - Dynamically change ellipse fill from c# 使用MVC 4和C#从sql数据库动态加载数据束 - Dynamically load bunches of data from sql database using MVC 4 and C# c#winformsdock填充树视图使它消失 - c# winforms dock fill tree-view make it disappear 使用C#从SQL数据库动态更改图表数据 - Changing chart data dynamically with C# from SQL database 使用NHibernate / C#从数据库中预加载树/层次结构 - preloading a tree / hierarchy from database using NHibernate / C# 使用C#从SQL数据库删除 - Deleting from SQL database using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM