Can Anybody Help?? Collections in C#

Last post 11-05-2009 1:59 AM by naveenj. 10 replies.

Sort Posts:

  • Can Anybody Help?? Collections in C#

    07-12-2007, 5:11 AM
    • Contributor
      4,845 point Contributor
    • naveenj
    • Member since 07-02-2007, 9:09 AM
    • India
    • Posts 877

    Hi,

    I am trying to convert a VB.net code to C# Embarrassed.

     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.

    Can anybody tell me a collection which takes objects and doesn't sort it. 

    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

    Try

    tvw.Nodes.Clear()

    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"))

    Next

    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)

    Else

    tvw.Nodes.Add(TvwNode) 'add node to Treeview

    End If

    Next

    Catch ex As Exception MsgBox("TreeView Population Error", MsgBoxStyle.Critical)

    End Try

    End Sub

     plz dont give me the online code converterSmile

    Thanks in Advance, Naveen

    Regards,
    Naveen

    Please remember to click Mark as Answer on the post that helps you
    View Blog
    Filed under: , ,
  • Re: Collections in C#

    07-12-2007, 5:35 AM
    Answer
    • Contributor
      3,121 point Contributor
    • sujitm
    • Member since 05-23-2007, 9:01 AM
    • Pune
    • Posts 512

    Hi,

    Try to use System.Collections.ArrayList or System.Collections.Generic.List<TreeNode>.

    - Sujit

    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Can Anybody Help?? Collections in C#

    07-13-2007, 12:35 AM
    • Contributor
      4,845 point Contributor
    • naveenj
    • Member since 07-02-2007, 9:09 AM
    • India
    • Posts 877

    Hi Guys, 

    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 matterSmile) since it is much faster.

    The Query is as follows

    CREATE PROCEDURE [dbo].[SP_RecursionEmp]
    AS

    BEGIN
    WITH RecEmp AS(SELECT emp_id,emp_name,parent_id,0 as depth
    FROM tbl_simple_employees
    UNION ALL
    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
    END

    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#.

    Regards,

    Naveen

    Regards,
    Naveen

    Please remember to click Mark as Answer on the post that helps you
    View Blog
  • Re: Can Anybody Help?? Collections in C#

    07-14-2007, 1:23 AM
    • Member
      624 point Member
    • jonathanparker
    • Member since 03-02-2006, 6:43 AM
    • Melbourne, Australia
    • Posts 103

    Reflector (http://www.aisto.com/roeder/dotnet/)

    would be the first thing I would try if I've got working code that I need to convert.

    1. Compile into DLL.

    2. Open DLL in Reflector.

    3. Select the language you want.

    4. Copy the code into VS

    5. Compile the code in the new language.

    HTH,

    Jonathan.
     

    MCTS in ASP.NET
  • Re: Can Anybody Help?? Collections in C#

    07-14-2007, 1:28 AM
    Answer
    • Member
      189 point Member
    • erikkl2000
    • Member since 06-05-2005, 5:55 AM
    • McKinney, TX.
    • Posts 160

     TreeNodeCollection tnc = new TreeNodeCollection();

    There’s my side and there’s your opinion
  • Re: Can Anybody Help?? Collections in C#

    07-16-2007, 7:55 PM
    • Member
      12 point Member
    • wasrari
    • Member since 07-16-2007, 11:39 PM
    • Bellingham, WA
    • Posts 9

    jonathanparker:

     Cosign! Reflector is great for this kind of stuff

    http://www.willasrari.com
  • Re: Can Anybody Help?? Collections in C#

    07-17-2007, 1:09 AM
    • Contributor
      4,845 point Contributor
    • naveenj
    • Member since 07-02-2007, 9:09 AM
    • India
    • Posts 877

    Hi guys,

    Thanks for all your response.

    For EveryBody who Suggested TreeNodeCollection and ArrayList.

    What is needed is a collection which

    1. Stores Data as object (or TreeNode)

    2. Stores Data as Key Value Pair

    3.Doesn't Automatically Sort with Key

     

    Thanks in Advance
    Naveen

    Regards,
    Naveen

    Please remember to click Mark as Answer on the post that helps you
    View Blog
  • Re: Can Anybody Help?? Collections in C#

    07-17-2007, 2:27 AM
    • Member
      624 point Member
    • jonathanparker
    • Member since 03-02-2006, 6:43 AM
    • Melbourne, Australia
    • Posts 103

    naveenj:

    The Code has been implemented in VB.
    I would like to have the Conversion in C#.

     

     

    So the VB code works? If so then your only problem is

    converting the code to C#. This should be very easy with Reflector (see above).

     
    If on the other hand you don't have all the VB code then you'll need to find or write a tree

    implementation in C# or VB as there isn't one in the .NET framework.

     

    HTH,

    Jonathan.
     

    MCTS in ASP.NET
  • Re: Can Anybody Help?? Collections in C#

    07-17-2007, 7:15 AM
    Answer

    Hi Naveen,

                 Try TreeNodeCollection.

     Regards,

    Sirisha Peddinti.

  • Re: Can Anybody Help?? Collections in C#

    10-20-2009, 10:08 AM
    Answer
    • Member
      420 point Member
    • vsdev
    • Member since 12-03-2007, 10:39 AM
    • Posts 74

    Naveen Try this

    this.tvEmployees.Nodes.Clear();
    Employees employees = new Employees();
    using (SqlConnection connection = new SqlConnection(@"Server=.\SQLEXPRESS; User Id=sa;Password=assyst@123;Database=work"))
    {
        connection.Open();
        using (SqlCommand command = new SqlCommand("usp_GetEmployees", connection))
        {
            command.CommandType = System.Data.CommandType.StoredProcedure;
            SqlDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            while (reader.Read())
            {
                employees.Add(new Employees.Employee()
                {
                    Depth = int.Parse(reader["depth"].ToString()),
                    EmployeeId = int.Parse(reader["EmployeeId"].ToString()),
                    Parent = int.Parse(reader["Parent"].ToString()),
                    EmployeeName = reader["EmployeeName"].ToString(),
                });
            }
        }
    }
    
    foreach (Employees.Employee employee in employees)
    {
        Employees.Employee parentEmp = employees.Find(o => o.EmployeeId == employee.Parent);
        if (parentEmp != null)
        {
            this.tvEmployees.Nodes.Find(parentEmp.EmployeeId.ToString(), true)[0].Nodes.Add(employee.EmployeeId.ToString(), employee.EmployeeName);
        }
        else
        {
            this.tvEmployees.Nodes.Add(employee.EmployeeId.ToString(),employee.EmployeeName);
        }
    }
    this.tvEmployees.ExpandAll();



    And the Employee class

    public class Employees : List<Employees.Employee>
    {
        public class Employee
        {
            public int EmployeeId
            {
                get;
                set;
            }
            public string EmployeeName
            {
                get;
                set;
            }
            public int Parent
            {
                get;
                set;
            }
            public int Depth
            {
                get;
                set;
            }
        }
    }



    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    If this solution doesn't solve your problem, just feel free to mark it as not answer and reply.
  • Re: Can Anybody Help?? Collections in C#

    11-05-2009, 1:59 AM
    • Contributor
      4,845 point Contributor
    • naveenj
    • Member since 07-02-2007, 9:09 AM
    • India
    • Posts 877

    Hi Anuraj,

    Thanks Smile

    Saw your blog post earlier: TreeView Population without recursive function

    Regards,
    Naveen

    Please remember to click Mark as Answer on the post that helps you
    View Blog
Page 1 of 1 (11 items)