问题描述
Hullo Good Guys,
我正在使用
使用VBNET2008,DataReader和TreeView的Window应用程序
我需要您的帮助,请帮助我.
我正在尝试在运行时使用DATAREADER填充TreeView控件
我正在使用TreeView控件,并且在进行编码时遇到了问题,因为我以前没有做过.
列出的beow是SQL字符串及其结果.还有TreeView的图形显示
SQL字符串:
SELECT OrderID,OrderDate ProductID
从发票在哪里(CustomerID ="Chops")按OrderID订购,Orderdate
SQL字符串的结果
OrderId OrderDate ProdID
10254 11/07/1996 74
10254 11/07/1996 24
10254 11/07/1996 55
10254 13/07/1996 203
10254 13/07/1996 112
10370 13/12/1996 74
10370 1996年3月12日1
10370 1996年3月12日64
10370 1996年3月12日56
10519 28/04/1997 10
10519 28/04/1997 60
TreeView显示格式
图形外观看起来像
10254 =>父节点
1996年11月7日=>子节点
74 =======> GrandChild节点
24
55
13/07/1996 =>子节点
203 ====> GrandChild节点
112
10370 =>父节点
1996年3月12日=>子节点
74 ====> GrandChild节点
1
64
56
下面列出的是我无法正常工作的编码
Hullo Good Guys,
I am using
Window Application using VBNET2008, DataReader and TreeView
I need your help, Please help me.
I am trying to use DATAREADER to fill the TreeView control during Runtime
I am using TreeView controls and having problem doing the coding as I have not done it before.
Listed beow are the SQL String and the result of it. And also the graphic drawing of TreeView display
SQL String :
SELECT OrderID, OrderDate ProductID
FROM Invoices WHERE (CustomerID = ''Chops'') Order by OrderID, Orderdate
Result of SQL String
OrderId OrderDate ProdID
10254 11/07/199674
10254 11/07/199624
10254 11/07/199655
10254 13/07/1996203
10254 13/07/1996 112
10370 13/12/199674
10370 03/12/19961
10370 03/12/199664
10370 03/12/199656
10519 28/04/199710
10519 28/04/199760
TreeView Display Format
Graphic drawing of how it should look like
10254 => Parent node
11/07/1996 =>Child node
74 =======> GrandChild node
24
55
13/07/1996 =>Child node
203 ====> GrandChild node
112
10370 => Parent node
03/12/1996 =>Child node
74 ====> GrandChild node
1
64
56
Listing below are my non working coding
<pre lang="vb">Private Sub btnDisplayTreeView_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnDisplayTreeView.Click
'' --variable to determine NEW NODE ---
Dim intOrderID As Integer = 0
Dim DteOrderDate As Date = Nothing
Dim intNode As Integer = 0
Dim intSubNode As Integer = 1
Dim strsql As String = Nothing
strsql &= "Select OrderID, Convert(varchar(10), OrderDate, 103) as [OrderDate], ProductID "
strsql &= " From OrderInvoices "
strsql &= " Where (CustomerID = N''" + strCustId + "'' )"
strsql &= " And ( OrderDate between @sDate and @eDate) "
strsql &= " Order by OrderId, OrderDate "
''define data object
sqlconn = New SqlConnection(connstr)
sqlcmd = New SqlCommand(strsql, sqlconn)
sqlcmd.Connection.Open()
''using sqlcmd parameter
With sqlcmd
.CommandText = strsql
.Parameters.Add("@sDate", SqlDbType.DateTime).Value = strFromDate
.Parameters.Add("@eDate", SqlDbType.DateTime).Value = strToDate
End With
DR = sqlcmd.ExecuteReader()
With Me.TreeViewInvoice
.BeginUpdate()
While (DR.Read())
推荐答案
<pre lang="sql">
With Me.TreeViewInvoice
.BeginUpdate()
While (DR.Read())
If (DR.Item("OrderID")<> intOrderID) Then
intNode += 0
intOrderID = DR.Item("OrderID")
'' ---- Parent ---OrderID
.Nodes.Add(DR.Item("OrderID"))
Else
''---- Child Level ----Order Date
If (DR.Item("OrderDate").ToString <> DteOrderDate.ToShortDateString) Then
DteOrderDate = Date.Parse(DR.Item("OrderDate").ToString)
''--- Child ---
.Nodes(intNode).Nodes.Add(DR.Item("OrderDate")) '' --child
End If
''---- GrandChild Level ----ProductId
.Nodes(intNode).Nodes(intSubNode).Nodes.Add(DR.Item("ProductID"))
End If
End While
End With
DR.Close()
sqlconn.Close()
sqlcmd.Dispose()
这篇关于VBNET2008在运行时使用DataReader填充TREEVIEW控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!