S
Scott
When populating a Treeview in code, I am getting "duplicate" nodes.
They appear in the treeview, but don't seem to exist when I iterate
through the node collections.
This is what I get:
http://www.emick.us/Capture.JPG
and this is my code. I am baffled.
Scott Emick
Public Class MainForm
Private Sub ProcessButton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ProcessButton.Click
Dim ds1 As New DS
Dim cn As New SqlConnection(My.Settings.WINDY)
cn.Open()
Dim cmd As New SqlCommand
cmd.Connection = cn
cmd.CommandText = "SELECT coalesce([MASTER NUMBER],-1) AS
MASTER_NUMBER FROM SALESTRANSACTIONS WHERE [SOP NUMBER] = '" &
OrderNumberTextBox.Text & "'"
Dim MasterNumber As Integer = cmd.ExecuteScalar
cmd.CommandText = "SELECT [SOP NUMBER], [ORIGINAL NUMBER] FROM
SALESTRANSACTIONS WHERE [MASTER NUMBER] = " & MasterNumber
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds1.OrderTree)
For rc As Integer = 0 To (ds1.OrderTree.Count - 1)
Dim dr As DS.OrderTreeRow = ds1.OrderTree(rc)
If dr.ORIGINAL_NUMBER = "" Then
OrderTreeView.Nodes.Add(dr.SOP_NUMBER)
Console.WriteLine(dr.SOP_NUMBER)
OrderTreeView.ExpandAll()
Application.DoEvents()
Else
'try to find the node to add to
For tc As Integer = 0 To (OrderTreeView.Nodes.Count -
1)
Dim rn As TreeNode = FindNode(dr.ORIGINAL_NUMBER,
OrderTreeView.Nodes(tc))
If Not rn Is Nothing Then
rn.Nodes.Add(dr.SOP_NUMBER)
OrderTreeView.ExpandAll()
Application.DoEvents()
Exit For
End If
Next
End If
Next
End Sub
Private Function FindNode(ByRef Original_Number As String, ByRef
nd As TreeNode) As TreeNode
If nd.Text = Original_Number Then
Return nd
End If
If nd.Nodes.Count > 0 Then
For Each sn As TreeNode In nd.Nodes
Dim rn As TreeNode = FindNode(Original_Number, sn)
If Not rn Is Nothing Then
Return rn
End If
Next
End If
Return Nothing
End Function
They appear in the treeview, but don't seem to exist when I iterate
through the node collections.
This is what I get:
http://www.emick.us/Capture.JPG
and this is my code. I am baffled.
Scott Emick
Public Class MainForm
Private Sub ProcessButton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ProcessButton.Click
Dim ds1 As New DS
Dim cn As New SqlConnection(My.Settings.WINDY)
cn.Open()
Dim cmd As New SqlCommand
cmd.Connection = cn
cmd.CommandText = "SELECT coalesce([MASTER NUMBER],-1) AS
MASTER_NUMBER FROM SALESTRANSACTIONS WHERE [SOP NUMBER] = '" &
OrderNumberTextBox.Text & "'"
Dim MasterNumber As Integer = cmd.ExecuteScalar
cmd.CommandText = "SELECT [SOP NUMBER], [ORIGINAL NUMBER] FROM
SALESTRANSACTIONS WHERE [MASTER NUMBER] = " & MasterNumber
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds1.OrderTree)
For rc As Integer = 0 To (ds1.OrderTree.Count - 1)
Dim dr As DS.OrderTreeRow = ds1.OrderTree(rc)
If dr.ORIGINAL_NUMBER = "" Then
OrderTreeView.Nodes.Add(dr.SOP_NUMBER)
Console.WriteLine(dr.SOP_NUMBER)
OrderTreeView.ExpandAll()
Application.DoEvents()
Else
'try to find the node to add to
For tc As Integer = 0 To (OrderTreeView.Nodes.Count -
1)
Dim rn As TreeNode = FindNode(dr.ORIGINAL_NUMBER,
OrderTreeView.Nodes(tc))
If Not rn Is Nothing Then
rn.Nodes.Add(dr.SOP_NUMBER)
OrderTreeView.ExpandAll()
Application.DoEvents()
Exit For
End If
Next
End If
Next
End Sub
Private Function FindNode(ByRef Original_Number As String, ByRef
nd As TreeNode) As TreeNode
If nd.Text = Original_Number Then
Return nd
End If
If nd.Nodes.Count > 0 Then
For Each sn As TreeNode In nd.Nodes
Dim rn As TreeNode = FindNode(Original_Number, sn)
If Not rn Is Nothing Then
Return rn
End If
Next
End If
Return Nothing
End Function