Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem adding child nodes in treeview

Posted on 2011-02-14
3
Medium Priority
?
765 Views
Last Modified: 2012-05-11
I am getting an exception thrown when trying to add a child node to a treeview. The data for the Parent node is held in an arraylist, the data for the Child nodes is held in an arraylist that is part of the parent array.

The exception is at ....salesorderIDArray.IndexOf(salesorders2)) .....

and is 'Specified argument was out of the range of valid values.
Parameter name: index'

The Parent node is shown correctly.
Public Class SalesOrders
	Inherits [Object]
	Private idOrd As Long
	Private ordNumber As Long
	Private ordDate As Date
	Private ordCustomerAcc As String = ""
	Private ordCustomerName As String = ""
	Private ordFwdQty As Integer = 0
	Private ordAllocQty As Integer = 0
	Private ordStatus As String = ""
	Friend ordSalesOrders As New ArrayList()

	Public Sub New(ByVal idorder As Long, ByVal ordernumber As Long, ByVal orderdate As Date, _
		ByVal ordercustacc As String, ByVal ordercustname As String, ByVal orderfwdqty As Integer, _
		ByVal orderallocqty As Integer, ByVal orderstatus As String)
		Me.idOrd = idorder
		Me.ordNumber = ordernumber
		Me.ordCustomerAcc = ordercustacc
		Me.ordCustomerName = ordercustname
		Me.ordDate = orderdate
		Me.ordFwdQty = orderfwdqty
		Me.ordStatus = orderstatus
		Me.ordAllocQty = orderallocqty
	End Sub

	Public Property IDSalesOrder() As Long
		Get
			Return Me.idOrd
		End Get
		Set(ByVal Value As Long)
			Me.idOrd = Value
		End Set
	End Property

	Public Property SalesOrderNumber() As Long
		Get
			Return Me.ordNumber
		End Get
		Set(ByVal Value As Long)
			Me.ordNumber = Value
		End Set
	End Property

	Public Property CustomerAccount() As String
		Get
			Return Me.ordCustomerAcc
		End Get
		Set(ByVal Value As String)
			Me.ordCustomerAcc = Value
		End Set
	End Property

	Public Property CustomerName() As String
		Get
			Return Me.ordCustomerName
		End Get
		Set(ByVal Value As String)
			Me.ordCustomerName = Value
		End Set
	End Property

	Public Property SalesOrderDate() As Date
		Get
			Return Me.ordDate
		End Get
		Set(ByVal Value As Date)
			Me.ordDate = Value
		End Set
	End Property

	Public Property SalesOrderFwdQty() As Integer
		Get
			Return Me.ordFwdQty
		End Get
		Set(ByVal Value As Integer)
			Me.ordFwdQty = Value
		End Set
	End Property

	Public Property SalesOrderAllocQty() As Integer
		Get
			Return Me.ordAllocQty
		End Get
		Set(ByVal Value As Integer)
			Me.ordAllocQty = Value
		End Set
	End Property

	Public Property SalesOrderStatus() As String
		Get
			Return Me.ordStatus
		End Get
		Set(ByVal Value As String)
			Me.ordStatus = Value
		End Set
	End Property

	Public ReadOnly Property SalesOrderLines() As ArrayList
		Get
			Return Me.ordSalesOrders
		End Get
	End Property
End Class 'End Company class

Public Class SalesOrderLine
	Inherits [Object]
	Private idOrdLine As Long
	Private ordlineOrderID As Long
	Private ordlinePrtSeq As Integer
	Private ordlineItmCode As String
	Private ordlineItmDesc As String
	Private ordlineLineQty As Integer
	Private ordlineAllocQty As Integer
	Private ordlineDespQty As Integer
	Private ordlineInvQty As Integer
	Private ordlinePromDelDte As Date
	Private ordlineFullFillMethID As Long

	Public Sub New(ByVal orderid As Long, ByVal ordlineid As Long, ByVal orderlineprtseq As Integer, ByVal orderlineitmcode As String, ByVal orderlineitmdesc As String, _
  ByVal orderlineordqty As Integer, ByVal orderlineallocqty As Integer, ByVal orderlinedespqty As Integer, ByVal orderlineinvqty As Integer, _
  ByVal orderlinepromdeldte As Date, ByVal orderlinefullfillmthdid As Long)
		Me.ordlineOrderID = orderid
		Me.idOrdLine = ordlineid
		Me.ordlinePrtSeq = orderlineprtseq
		Me.ordlineItmCode = orderlineitmcode
		Me.ordlineItmDesc = orderlineitmdesc
		Me.ordlineLineQty = orderlineordqty
		Me.ordlineAllocQty = orderlineallocqty
		Me.ordlineDespQty = orderlinedespqty
		Me.ordlineInvQty = orderlineinvqty
		Me.ordlinePromDelDte = orderlinepromdeldte
		Me.ordlineFullFillMethID = orderlinefullfillmthdid
	End Sub 'New

	Public Property SalesOrderID() As Long
		Get
			Return Me.ordlineOrderID
		End Get
		Set(ByVal Value As Long)
			Me.ordlineOrderID = Value
		End Set
	End Property

	Public Property IDOrderLine() As Long
		Get
			Return Me.idOrdLine
		End Get
		Set(ByVal Value As Long)
			Me.idOrdLine = Value
		End Set
	End Property

	Public Property PrintSequence() As Integer
		Get
			Return Me.ordlinePrtSeq
		End Get
		Set(ByVal Value As Integer)
			Me.ordlinePrtSeq = Value
		End Set
	End Property

	Public Property ItemCode() As String
		Get
			Return Me.ordlineItmCode
		End Get
		Set(ByVal Value As String)
			Me.ordlineItmCode = Value
		End Set
	End Property

	Public Property ItemDescription() As String
		Get
			Return Me.ordlineItmDesc
		End Get
		Set(ByVal Value As String)
			Me.ordlineItmDesc = Value
		End Set
	End Property

	Public Property LineQuantity() As Integer
		Get
			Return Me.ordlineLineQty
		End Get
		Set(ByVal Value As Integer)
			Me.ordlineLineQty = Value
		End Set
	End Property

	Public Property AllocatedQuantity() As Integer
		Get
			Return Me.ordlineAllocQty
		End Get
		Set(ByVal Value As Integer)
			Me.ordlineAllocQty = Value
		End Set
	End Property

	Public Property DespatchQuantity() As Integer
		Get
			Return Me.ordlineDespQty
		End Get
		Set(ByVal Value As Integer)
			Me.ordlineDespQty = Value
		End Set
	End Property

	Public Property InvoicedQuantity() As Integer
		Get
			Return Me.ordlineInvQty
		End Get
		Set(ByVal Value As Integer)
			Me.ordlineInvQty = Value
		End Set
	End Property

	Public Property PromisedDeliveryDate() As Date
		Get
			Return Me.ordlinePromDelDte
		End Get
		Set(ByVal Value As Date)
			Me.ordlinePromDelDte = Value
		End Set
	End Property

	Public Property FullFillmentMethod() As Long
		Get
			Return Me.ordlineFullFillMethID
		End Get
		Set(ByVal Value As Long)
			Me.ordlineFullFillMethID = Value
		End Set
	End Property

End Class ' End SalesOrderLines class

'+++++++++++++++++
			If salesorderIDArray.Count = 0 Then
				tvwNodeFwdOrders = Me.tvwForwardOrders.Nodes.Add("No Forward Orders for Item")
				tvwNodeFwdOrders.Tag = "*"
				tvwNodeFwdOrders.ForeColor = Color.Red
			Else

				For Each salesorders2 In salesorderIDArray

					tvwNodeFwdOrders = Me.tvwForwardOrders.Nodes.Add(salesorders2.SalesOrderDate.ToShortDateString + _
					"  [" + salesorders2.CustomerAccount.PadLeft(8, CChar(" ")) + "]  " + salesorders2.CustomerName.PadRight(30, CChar(" ")) + _
					 " ~ Order: " + salesorders2.SalesOrderNumber.ToString.PadLeft(8, CChar("0")) + _
					 "   Alloc Qty: " + salesorders2.SalesOrderAllocQty.ToString + _
					 "   Fwd Qty: " + salesorders2.SalesOrderFwdQty.ToString + "  [" + salesorders2.SalesOrderStatus.ToString + "]")

					tvwNodeFwdOrders.Tag = salesorders2.IDSalesOrder.ToString()
					tvwNodeFwdOrders.ForeColor = Color.Blue
					'Me.tvwForwardOrders.HideSelection = True

					Dim salesorderline1 As SalesOrderLine

					' Loop through the Order Lines array and add child nodes
					For Each salesorderline1 In salesorders2.SalesOrderLines
						tvwNodeChild = tvwNodeFwdOrders.Nodes(salesorderIDArray.IndexOf(salesorders2)).Nodes.Add("Line: " + salesorderline1.PrintSequence.ToString)
						tvwNodeChild.Tag = salesorderline1.IDOrderLine.ToString
					Next salesorderline1

				Next salesorders2

			End If ' companyArray.Count

Open in new window

0
Comment
Question by:TeDeSm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
wellhole earned 2000 total points
ID: 34888450
Notice this line that you have above your error line:

tvwNodeFwdOrders = Me.tvwForwardOrders.Nodes.Add(salesorders2.SalesOrderDate.ToShortDateString + _
                              "  [" + salesorders2.CustomerAccount.PadLeft(8, CChar(" ")) + "]  " + salesorders2.CustomerName.PadRight(30, CChar(" ")) + _
                               " ~ Order: " + salesorders2.SalesOrderNumber.ToString.PadLeft(8, CChar("0")) + _
                               "   Alloc Qty: " + salesorders2.SalesOrderAllocQty.ToString + _
                               "   Fwd Qty: " + salesorders2.SalesOrderFwdQty.ToString + "  [" + salesorders2.SalesOrderStatus.ToString + "]")


It's reassigning tvwNodeFwdOrders as a child node of the original tvwNodeFwdOrders node. So, if you try to access a child node via tvwNodeFwdOrders.Nodes(salesorderIDArray.IndexOf(salesorders2)) you'll get the exception because tvwNodeFwdOrders does not have a child node at that point.
0
 

Author Comment

by:TeDeSm
ID: 34888609
Thanks for your fresh pair of eyes, I had got 'code blind' and totally missed the error. Perhaps a change in variable names would have helped me here in distinguishing what was the TreeView and the 'New Nodes To Add'.

The below code works.
' Add a root TreeNode for each object in the ArrayList.
			Dim salesorders2 As SalesOrders
			Dim tvwNodeFwdOrders As TreeNode
			Dim tvwNodeChild As TreeNode

			If salesOrderArray.Count = 0 Then
				tvwNodeFwdOrders = Me.tvwForwardOrders.Nodes.Add("No Forward Orders for Item")
				tvwNodeFwdOrders.Tag = "*"
				tvwNodeFwdOrders.ForeColor = Color.Red
			Else

				For Each salesorders2 In salesOrderArray

					' Loop through the Order array and add parent nodes
					tvwNodeFwdOrders = tvwForwardOrders.Nodes.Add(salesorders2.SalesOrderDate.ToShortDateString + _
					 "  [" + salesorders2.CustomerAccount.PadLeft(8, CChar(" ")) + "]  " + salesorders2.CustomerName.PadRight(30, CChar(" ")) + _
					  " ~ Order: " + salesorders2.SalesOrderNumber.ToString.PadLeft(8, CChar("0")) + _
					  "   Alloc Qty: " + salesorders2.SalesOrderAllocQty.ToString + _
					  "   Fwd Qty: " + salesorders2.SalesOrderFwdQty.ToString + "  [" + salesorders2.SalesOrderStatus.ToString + "]")

					tvwNodeFwdOrders.Tag = salesorders2.IDSalesOrder
					tvwNodeFwdOrders.ForeColor = Color.Blue
					'Me.tvwForwardOrders.HideSelection = True

					Dim salesorderline1 As SalesOrderLine

					' Loop through the Order Lines array and add child nodes
					For Each salesorderline1 In salesorders2.SalesOrderLines
						tvwNodeChild = tvwForwardOrders.Nodes(salesOrderArray.IndexOf(salesorders2)).Nodes.Add("Line: " + salesorderline1.PrintSequence.ToString)
						tvwNodeChild.Tag = salesorderline1.IDOrderLine
					Next salesorderline1

				Next salesorders2

			End If ' companyArray.Count

Open in new window

0
 

Author Closing Comment

by:TeDeSm
ID: 34888618
Great help, thanks
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question