将表值添加到树视图? [英] Adding table values to a treeview?

查看:91
本文介绍了将表值添加到树视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Vb 2008快递版,对于树视图来说我是一个新手。我对如何连接数据库有基本的了解。我正在使用的数据库是一个微软访问数据库,并有大量的表与各种信息。我需要将其中两个表放入树视图中。一个有2列称为日期和日期ID,日期将是树视图上的主要节点。另一个表有8个列,其中包括第一个表中的相应日期ID,采购订单ID和采购订单号。子节点将是采购订单编号。



现在我知道通过微软访问数据库在树视图上有一堆教程,但我没有找到具体的根据我的需要,他们只是将数据库中的所有数据转储到表中。我只想要两个表的具体内容。如果有人可以帮我解决这个问题,我将非常感激。如果需要我可以提供更多信息我正在使用什么或其他什么。!



这里是我现在的代码,有两个我一直在尝试的变种两者都包括在内



  Imports  System.Data 
< span class =code-keyword> Imports System.Data.OleDb
Imports System.Data.SqlClient

公开 Form1
Dim mypath = I:\ Casegoods \Database\Expertpetrolia.mdb
Dim conn 作为 OleDbConnection( Provider = microsoft.ace.oledb.12.0; Data Source =& mypath)
< span class =code-ke yword> Dim cmd 作为 OleDbCommand
Dim dr As OleDbDataReader
Dim sql1 = SELECT * FROM Supply_Date ORDER BY [Due_Date]
Dim sql2 = SELECT * FROM Supply_Date INNER JOIN Total_Purchase ON Supply_Date.Date_Id = Total_Purchase.Date_Id ORDER BY [Due_Date]

Dim dr2 As OleDbDataReader

' Dim sql2 =SELECT * FROM Total_Purchase ORDER BY [Date_Id]
Dim cmd2 作为 OleDbCommand






私有 Sub Form1_Load( ByVal sender 作为系统。对象 ByVal e As System.EventArgs)句柄 MyBase .Load


Dim cmd 作为 OleDbCommand
使用 conn



cmd = OleDbCommand(sql1,conn)
cmd2 = OleDbCommand(sql2,conn)
conn.Open()

dr = cmd.ExecuteReader
dr2 = cmd2.ExecuteReader

如果 dr.HasRows 那么
dr.Read()
Dim parent As String =(dr.Item( Due_Date))

如果 DateDiff(Microsoft .VisualBasic.DateInterval.Day,今天,(dr.Item( Due_Date)))> -10 然后
TreeView1.Nodes.Add(父)
如果(dr2 .Item( Date_Id))=(dr.Item( Date_Id))然后
TreeView1.Nodes(parent) .Nodes.Add(dr2.Item( Purchase_Order_Number))

结束 如果

结束 如果


循环
< span class =code-keyword> Else
MsgBox( 找不到任何东西
结束 如果



dr.Close()

结束 使用

结束 Sub
' Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load


' Dim cmd As OleDbCommand
' 使用conn



' cmd =新OleDbCommand(sql1,conn)
' 'cmd2 =新的OleDbCommand(sql2,conn)
' conn.Open()


' Dim dr As OleDbDataReader = cmd.ExecuteReader
' 'Dim dr2 As OleDbDataReader = cmd2.ExecuteReader

' do dr dr.Read()
' Dim str1 As String = dr.Item(Due_Date)
' Dim str2 As String = dr。项目(Date_Id)
' 尝试
' Dim str3 As String = dr.Item(date_id)
' Dim str4 As String = dr.Item(Purchase_Order_Number)
' 如果DateDiff(Microsoft.VisualBasic.DateInterval.Day,Today,(dr.Item(Due_Date)))> -10然后
' TreeView1.Nodes.Add(str1)
' 如果str2 = str3那么
' TreeView1.Nodes(0).Nodes.Add(New TreeNode(str4))
' 结束如果
' 结束如果
' Catch ex As Exception
' MsgBox(ex.Message,MsgBoxStyle.Critical,stumbid dims)
' 结束尝试


' 循环

' 结束使用

' 结束子
结束

解决方案

看看这里:使用HierarchyID加载TreeView [ ^ ]。

sql server数据库有一个示例算法实现,但主要思路是一样的。您需要根据需要进行更改。


您必须使用此代码进行一些操作才能使其正常工作。

数据库连接和数据选择:

  Dim 连接 As   Data.OleDb.OleDbConnection(  Provider = Microsoft.Jet.OLEDB.4.0; Data Source = YourDatabseName.mdb;
Dim 命令作为 Data.OleDb.OleDbCommand( SELECT * FROM TableName,Connection)

Connection.Open()
Dim Reader As OleDbDataReader = Command.ExecuteReader

< span class =code-keyword> while Reader.Read()
Dim str1 As String = Reader.Item( Column1Name
Dim str2 As String = Reader.Item( Column2Name
Dim str3 As String = Reader.Item( Column3Name
循环

Connection.Close()





至于更新树视图:

 TreeView1.Nodes.Add( TreeNode(  NewNode))
TreeView1.Nodes( 0 )。Nodes.Add( TreeNode( ChildNode))
TreeView1.Nodes( 0 )。Nodes.Add(< span class =code-keyword>新
TreeNode( ChildNode))
TreeView1.Nodes( 0 )。节点( 0 )。Nodes.Add( TreeNode( ChildChildNode))


I am using Vb 2008 express edition and am very new to treeviews. i have basic knowledge of how to connect to a database. the database i am working with is a microsoft access database and has a large amount of tables with various information. two of these tables i need to put into a treeview. one has 2 columns called date and date id, the date will be the main nodes on the treeview. the other table has 8 colums, among them are the corresponding date id's from the first table, the purchase order id and the purchase order number. the child nodes will be the purchase order number.

Now i know there are a bunch of tutorials out there on treeview population through microsoft access databases but i have found none specifically with what i need, they are all just about dumping ALL the data from the database into the table. i just want specific contents of two tables. if someone could help me out with this i would be very grateful. i can give more information if needed on what i am working with or anything else.!

here is my code as of now, there are two variants i have been trying both are included

Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient

Public Class Form1
    Dim mypath = "I:\Casegoods\Database\Expertpetrolia.mdb"
    Dim conn As New OleDbConnection("Provider = microsoft.ace.oledb.12.0;Data Source=" & mypath)
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim sql1 = "SELECT * FROM Supply_Date ORDER BY [Due_Date]"
    Dim sql2 = "SELECT * FROM Supply_Date INNER JOIN Total_Purchase ON Supply_Date.Date_Id = Total_Purchase.Date_Id ORDER BY [Due_Date]"

    Dim dr2 As OleDbDataReader

    'Dim sql2 = "SELECT * FROM Total_Purchase ORDER BY [Date_Id]"
    Dim cmd2 As OleDbCommand






    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Dim cmd As OleDbCommand
        Using conn



            cmd = New OleDbCommand(sql1, conn)
            cmd2 = New OleDbCommand(sql2, conn)
            conn.Open()

            dr = cmd.ExecuteReader
            dr2 = cmd2.ExecuteReader

            If dr.HasRows Then
                Do While dr.Read()
                    Dim parent As String = (dr.Item("Due_Date"))

                    If DateDiff(Microsoft.VisualBasic.DateInterval.Day, Today, (dr.Item("Due_Date"))) > -10 Then
                        TreeView1.Nodes.Add(parent)
                        If (dr2.Item("Date_Id")) = (dr.Item("Date_Id")) Then
                            TreeView1.Nodes(parent).Nodes.Add(dr2.Item("Purchase_Order_Number"))

                        End If

                    End If


                Loop
            Else
                MsgBox("No Things found")
            End If



            dr.Close()

        End Using

    End Sub
    'Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    '    Dim cmd As OleDbCommand
    '    Using conn



    '        cmd = New OleDbCommand(sql1, conn)
    '        'cmd2 = New OleDbCommand(sql2, conn)
    '        conn.Open()


    '        Dim dr As OleDbDataReader = cmd.ExecuteReader
    '        ' Dim dr2 As OleDbDataReader = cmd2.ExecuteReader

    '        Do While dr.Read()
    '            Dim str1 As String = dr.Item("Due_Date")
    '            Dim str2 As String = dr.Item("Date_Id")
    '            Try
    '                Dim str3 As String = dr.Item("date_id")
    '                Dim str4 As String = dr.Item("Purchase_Order_Number")
    '                If DateDiff(Microsoft.VisualBasic.DateInterval.Day, Today, (dr.Item("Due_Date"))) > -10 Then
    '                    TreeView1.Nodes.Add(str1)
    '                    If str2 = str3 Then
    '                        TreeView1.Nodes(0).Nodes.Add(New TreeNode(str4))
    '                    End If
    '                End If
    '            Catch ex As Exception
    '                MsgBox(ex.Message, MsgBoxStyle.Critical, "stupid dims")
    '            End Try


    '        Loop

    '    End Using

    'End Sub
End Class

解决方案

Have a look here: Loading a TreeView using HierarchyID[^].
There is a sample algorithm implementation for sql server database, but the main idea is the same. You need to change it to your needs.


You'll have to play around a bit with this code to get it working for you.
Database connection and data selection:

Dim Connection As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=YourDatabseName.mdb;")
Dim Command As New Data.OleDb.OleDbCommand("SELECT * FROM TableName", Connection)

Connection.Open()
Dim Reader As OleDbDataReader = Command.ExecuteReader

Do While Reader.Read()
    Dim str1 As String = Reader.Item("Column1Name")
    Dim str2 As String = Reader.Item("Column2Name")
    Dim str3 As String = Reader.Item("Column3Name")
Loop

Connection.Close()



As for updating the treeview:

TreeView1.Nodes.Add(New TreeNode("NewNode"))
TreeView1.Nodes(0).Nodes.Add(New TreeNode("ChildNode"))
TreeView1.Nodes(0).Nodes.Add(New TreeNode("ChildNode"))
TreeView1.Nodes(0).Nodes(0).Nodes.Add(New TreeNode("ChildChildNode"))


这篇关于将表值添加到树视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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