By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,905 Members | 1,616 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,905 IT Pros & Developers. It's quick & easy.

ID Column in Treeview

P: n/a
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
Feb 15 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ben wrote:
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.
Feb 15 '07 #2

P: n/a
You could use the Handle property.

T

Ben wrote:
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
Feb 15 '07 #3

P: n/a
"tomb" <to**@technetcenter.comschrieb:
You could use the Handle property.
I assume you are referring to the 'TreeNode' objects' 'Tag' property.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Feb 16 '07 #4

P: n/a
Ben wrote:
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.
Feb 16 '07 #5

P: n/a
Ben
Thank you all for your input

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

Many thanks
B

"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
news:er**********@south.jnrs.ja.net...
Ben wrote:
>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.

Feb 16 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.