• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 903
  • Last Modified:

How to fill a treeview from a datatable with recursive values

Hi,
 I have a datatable with recursive values that I want to display into a treeview. How can I acomplish that in c sharp?

Please see snippet
Thanks in advance
This is my datatable values

ParentAssembly_ID Assembly_ID Quantity    
----------------- ----------- -----------
1864              1865        2           
1865              1866        1           
1865              1867        1           
1865              1868        1           
1865              1869        3           
1865              1870        1           
1870              1871        1           
1864              1872        1
1872              1873        1

This is how I need it displayed in the treeview

- 1864
   - 1865
     -- 1866
     -- 1867
     -- 1868
     -- 1869
     -- 1869
     -- 1870
       --- 1871
   - 1872
     -- 1873

Open in new window

0
negreteo
Asked:
negreteo
  • 5
  • 5
1 Solution
 
Bob LearnedCommented:
The steps are slightly different between an ASP.NET web TreeView and a Windows Forms TreeView, so it might help to know which one you are working with...
0
 
negreteoAuthor Commented:
oh it is a Windows Forms TreeView
0
 
Bob LearnedCommented:
1) Get the data with a data reader, ordered by Assembly_ID, so that the nodes will get created in order.

2) Loop through the data reader, and get data elements

3) Create a new TreeNode, setting the Text and Name property (Name so that you can find the node).

4) If the data doesn't have a parent ID, then add it to the root

5) Otherwise, try to find the parent TreeNode.  

6) If the parent node is found, then add the new TreeNode to the parent's Nodes collection.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
negreteoAuthor Commented:
Can you please show me some code that I can use as reference, that's what I am mainly looking for
Thanks
0
 
negreteoAuthor Commented:
unfortunately this example is for a web app and I get error with code

 s.Push(lastNode.ChildNodes);

Got an example for windows forms?

Thanks
0
 
Bob LearnedCommented:
Here is an untested, example class:


using System.Data.SqlClient; 
using System.Windows.Forms; 

public class TreeDataReader 
{ 
    
    private SqlDataReader m_reader; 
    private TreeView m_treeView; 
    private string m_connectionString = ""; 
    private string m_commandText = ""; 
    private string m_parentIDField = null; 
    private string m_idField = null; 
    private string m_textField = null; 
    
    public TreeDataReader(string connectionString, string commandText, TreeView treeView, string parentIDField, string idField, string textField) 
    { 
        
        m_connectionString = connectionString; 
        m_commandText = commandText; 
        m_treeView = treeView; 
        m_parentIDField = parentIDField; 
        m_idField = idField; 
        m_textField = textField; 
        
        if (string.IsNullOrEmpty(m_connectionString)) { 
            throw new ArgumentNullException("Connection string"); 
        } 
        
        if (string.IsNullOrEmpty(m_commandText)) { 
            throw new ArgumentNullException("Command text"); 
        } 
        
        if (string.IsNullOrEmpty(m_parentIDField)) { 
            throw new ArgumentNullException("Parent ID field"); 
        } 
        
        if (string.IsNullOrEmpty(m_idField)) { 
            throw new ArgumentNullException("ID field"); 
        } 
        
        if (string.IsNullOrEmpty(m_textField)) { 
            throw new ArgumentNullException("Text field"); 
        } 
        
        if (m_treeView == null) { 
            throw new ArgumentNullException("Tree"); 
            
        } 
    } 
    
    private void LoadTree(string connectionString, string commandText) 
    { 
        using (SqlConnection connection = new SqlConnection(connectionString)) { 
            connection.Open(); 
            
            using (SqlCommand command = new SqlCommand()) { 
                using (SqlDataReader reader = command.ExecuteReader()) { 
                    
                    while (reader.Read) { 
                        int parentID = Conversion.Val(reader(m_parentIDField)); 
                        int ID = Conversion.Val(reader(m_idField)); 
                        string text = reader(m_textField); 
                        
                        TreeNode node = new TreeNode(text); 
                        node.Name = ID; 
                        
                        if (parentID == -1) { 
                            m_treeView.Nodes.Add(node); 
                        } 
                        else { 
                            TreeNode[] parentNodeList = m_treeView.Nodes.Find(parentID, true); 
                            
                            if (parentNodeList.Length > 0) { 
                                parentNodeList(0).Nodes.Add(node); 
                            } 
                        } 
                    } 
                } 
            } 
        } 
    } 
    
} 

Open in new window

0
 
negreteoAuthor Commented:
OK I'll try to convert to c# and try it

Thanks
0
 
Bob LearnedCommented:
C# code attached.
0
 
negreteoAuthor Commented:
It works!!

Actually I already had the values in a datatable so I just did some modifications in the code

Thanks a lot
//Populate treeView from datatable
                foreach (DataRow drBOM in dt.Rows)
                {
                    int ParentID = Convert.ToInt32(drBOM["ParentAssembly_ID"].ToString());
                    int ID = Convert.ToInt32(drBOM["Assembly_ID"].ToString());

                    TreeNode node = new TreeNode(drBOM["Assembly_ID"].ToString());
                    node.Name = ID.ToString();

                    if (ParentID == Model_ID)
                    {
                        treeBOM.Nodes.Add(node);
                    }//if
                    else
                    {
                        TreeNode[] parentNodeList = treeBOM.Nodes.Find(ParentID.ToString(), true);
                        if (parentNodeList.Length > 0)
                        {
                            parentNodeList[0].Nodes.Add(node);
                        }//if
                    }//else
                }//foreach

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now