Wrox Programmer Forums
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 12th, 2007, 04:01 AM
Registered User
Join Date: Jul 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can anybody help? Collection in C#

I am trying to convert a VB.net code to C#.
it is to bind a treeview from a collection.
but not finding a Collection which stores treenodes as object.
Cant use hashtable since it automatically sorts the data by key.

The code is as follows
can anybody convert this?

Private Sub TreeViewPopulation(ByVal ds As DataSet, Optional ByVal intParent As Integer = 0)
Dim TvwParent As New TreeNode
Dim TvwNode As New TreeNode
Dim CollItems As New Collection

For Each dr As DataRow In ds.Tables(0).Rows ''''assigning dataset values to a collection as node
TvwNode = New TreeNode(dr("Employee_Nm").ToString)
TvwNode.Tag = dr("Parent")
CollItems.Add(TvwNode, dr("Employee_Id"))
For Each TvwNode In CollItems
TvwParent = Nothing
'find parent of a node
If Val(TvwNode.Tag) <> intParent Then TvwParent = CType(CollItems.Item(TvwNode.Tag.ToString), TreeNode)
If Not TvwParent Is Nothing Then
'add child node to its parent node
CType(CollItems.Item(TvwNode.Tag.ToString), TreeNode).Nodes.Add(TvwNode.Clone)
tvw.Nodes.Add(TvwNode) 'add node to Treeview
End If

End Sub

plz dont give me the online code converter:)
Thanks in Advance,

�You can cut all the flowers but you cannot keep spring from coming.�
Old July 12th, 2007, 08:24 AM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

What about the TreeNodeCollection class which "Represents a collection of TreeNode objects"?


Old July 12th, 2007, 11:34 PM
Registered User
Join Date: Jul 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks for the Reply Peter.
But its not what is needed.
A TreeNodeCollection is just a collection of TreeNodes.
Whats needed is a Collection that stores object in a key value pair format which doesn't sort the objects by key.

An Elaboration on the program.

Here TreeView is being populated without recursive function using
a Single Recursive Query to get the child nodes of corresponding parents (and grandparents for that matter:)) since it is much faster.

The Query is as follows

CREATE PROCEDURE [dbo].[SP_RecursionEmp]

WITH RecEmp AS(SELECT emp_id,emp_name,parent_id,0 as depth
FROM tbl_simple_employees
SELECT E.emp_id,E.emp_name,E.parent_id,R.depth+1 AS depth FROM
tbl_simple_employees AS E INNER JOIN RecEmp AS R ON R.emp_id=E.parent_id )
SELECT max(emp_id)as emp_id,max(emp_name)as emp_name,max(parent_id)as parent_id,max(depth)as depth FROM RecEmp r group by emp_id order by depth

now i took the result in a DataSet and passed it to a
custom created function as follows

    private void TreeViewPopulation(DataSet ds)

what I need is a C# Code to implement it.
The Code has been implemented in VB.
I would like to have the Conversion in C#.

�You can cut all the flowers but you cannot keep spring from coming.�
Old July 13th, 2007, 03:31 AM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

Hi Naveen,

What about a generics Dictionary class? E.g.:

Dictionary<string, TreeNode> collItems = new Dictionary<string, TreeNode>();

This allows you to add TreeNode objects by key to the dictionary. You'll need to import the System.Collections.Generic namespace for this.

Alternatively, look at the other generic class in the BCL:


and see if any of them are usable for you.



Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Old July 15th, 2007, 09:34 PM
Registered User
Join Date: Jul 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts

Hi Imar,
This method sounds intriguing.
Sorry for replying late.
No work on weekend, thats why.

You can cut all the flowers but you cannot keep spring from coming -Pablo Neruda

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the collection of forms ramesh_cnj .NET Framework 1.x 0 February 27th, 2006 08:47 AM
Collection in C# ? dedex C# 1 January 14th, 2005 04:51 PM
COM Collection ben0027 Visual C++ 0 May 1st, 2004 05:19 AM
Help collection gone MikeB VS.NET 2002/2003 2 June 12th, 2003 02:33 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.