ID Column in Treeview

  • Thread starter Thread starter Ben
  • Start date Start date
B

Ben

Hi

We are using the treeview control unbound.

We are populating it from a database but would like to add a hidden
reference/ID column. Is this possible?

Thanks
B
 
Ben said:
Hi

We are using the treeview control unbound.

We are populating it from a database but would like to add a hidden
reference/ID column. Is this possible?

Thanks
B

You can use the Tag property to store data.
 
Ben said:
We are populating it from a database but would like to add a hidden
reference/ID column. Is this possible?

Yes, you can, but personally, I don't do it that way any more.

The VB6 TreeView used to have a Key property on every Node.
We used this to "lookup" the "record" (or whatever) that held data about
this particular item.
So, you started with a Node, got it's Key, then went off to /another/
data store to get the data.

Here's an alternative.

Hold each data "record" in a Class that derives from TreeNode.
Instantiate these and add them, directly, into the TreeView.

Or:

Hold each "record" in a class from which you can /obtain/ (i.e. has a
Property that returns) an object that derives from TreeNode and has a
Property "pointing back" to the "record" object).

Now, there's no need to go looking in other data stores - all the data
about whatever it is is encapsulated within that class.

Class CustomTreeNode
Inherits TreeNode

Private Sub New()
End Sub

Public Sub New( id as ... )
MyBase.New()
m_sId = id
End Sub

Public Property ID() As String
Get
Return m_sId
End Get
End Property

Private m_sId As String

End Class

....then...

For Each dr as DataRow In dtThings
Dim oNode as New CustomTreeNode( dr.Item( "ID" ).ToString() )
tvTree.Nodes.Add( oNode )
Next

Private Sub tvTree_AfterSelect( ... ) _
Handles tvTree.AfterSelect

If TypeOf e.Node Is CustomTreeNode Then
' Handle one of your custom nodes
Else
' Deal with an ordinary one
End If

End Sub

HTH,
Phill W.
 
Thank you all for your input

I have used the Tag, but will look into the class for future usage.

Many thanks
B
 
Back
Top