使用Access数据库中的数据填充TreeView [英] Fill a TreeView with data from an Access DB

查看:342
本文介绍了使用Access数据库中的数据填充TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们!

我在Access中有一个数据库,它看起来像这样



Hi friends!
I have a DB in Access, it looks like this

Field1    Field2
100000    Active
110000    Current Active
111000    Availavilities
111100    Money Box
111101    MoneyBox1
111102    MoneyBox2
111103    MoneyBox3



有了这个数据我想填充一个树视图

看起来应该是这样的




With this Data i want to Populate a Treeview
It should look like this

Active
|--Current Active
   |--Availavilities
      |--Money Box
         |--MoneyBox1
         |--MoneyBox2
         |--MoneyBox3





我正在使用这段代码(我错过了将节点编写为的所有逻辑我想要)



非常感谢!!!!!!!!!!







I'm working with this code (i'm missing all the logic to write the nodes as i want)

Thank you so much!!!!!!!!!!


Private Sub FormTreeViewPC_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    '//LOADS ALL THE DATA
    tv.Nodes.Clear()

    CMD.Connection = conn
    CMD.CommandType = CommandType.Text

    SQL = "SELECT FIELD1, FIELD2 FROM PLANCUENTAS" & cuitClient & ""

    CMD.CommandText = SQL
    Try
        dr = CMD.ExecuteReader

        While dr.Read

        End While
        dr.Close()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

推荐答案

我假设ID(Field1)值是整数而不是字符串。

如果按Field1排序,任务可以一次完成,但你没有说,所以我假设你这样做。

我想使用字典< int,TreeNode> 来保存e节点。



0)实例化字典

1)对于DataReader中的每条记录

1.1)实例化TreeNode以保存数据

1.2)将TreeNode添加到字典

1.3)搜索父级(X = 10)

1.3.1)将ID除以X,然后乘以X

1.3.2)如果新值为零,则没有父项 - 将节点添加到树的节点集合

1.3.3)如果字典中存在新值,我们找到父节点,将节点添加到父节点集合中

1.3.4)否则将X乘以10并重复1.3.1





(编辑:重写父ID搜索算法。我可能需要尝试以确保它是正确的。)
I'll assume the ID (Field1) values are integers rather than strings.
If you sort by Field1 the task can be done in one pass, but you don't say, so I'll assume you do.
I'd use a Dictionary<int,TreeNode> to hold the nodes.

0) Instantiate a Dictionary
1) For each record in the DataReader
1.1) Instantiate a TreeNode to hold the data
1.2) Add the TreeNode to the Dictionary
1.3) Search for a parent (X=10)
1.3.1) Divide the ID by X, then multiply by X
1.3.2) If the new value is zero, there is no parent -- add the node to the Tree's Nodes collection
1.3.3) Else if the new value exists in the Dictionary, we have found the parent, add the node to the parent's Nodes collection
1.3.4) Else multiply X by 10 and repeat from 1.3.1


( Reworked the parent ID search algorithm. I may need to try it to be sure it's correct.)


这篇关于使用Access数据库中的数据填充TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆