Solved

Problem adding child nodes in treeview

Posted on 2011-02-14
3
759 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
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
wellhole earned 500 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

832 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